国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python加載帶有注釋的Json文件實例

2020-02-23 00:13:22
字體:
來源:轉載
供稿:網友

由于json文件不支持注釋,所以如果在json文件中標記了注釋,則使用python中的json.dump()無法加載該json文件。

本文旨在解決當定義“//”為json注釋時,如何正確解析有注釋的json文件。

程序實現

# encoding: utf-8import jsonimport reimport sysreload(sys)sys.setdefaultencoding('utf8')CAUTION_PRINT_HEAD = 'caution: '# 創建一個xstr類,用于處理從文件中讀出的字符串class xstr: def __init__(self, instr):  self.instr = instr # 刪除“//”標志后的注釋 def rmCmt(self):   qtCnt = cmtPos = slashPos = 0  rearLine = self.instr  # rearline: 前一個“//”之后的字符串,  # 雙引號里的“//”不是注釋標志,所以遇到這種情況,仍需繼續查找后續的“//”  while rearLine.find('//') >= 0: # 查找“//”   slashPos = rearLine.find('//')   cmtPos += slashPos   # print 'slashPos: ' + str(slashPos)   headLine = rearLine[:slashPos]   while headLine.find('"') >= 0: # 查找“//”前的雙引號    qtPos = headLine.find('"')    if not self.isEscapeOpr(headLine[:qtPos]): # 如果雙引號沒有被轉義     qtCnt += 1 # 雙引號的數量加1    headLine = headLine[qtPos+1:]    # print qtCnt   if qtCnt % 2 == 0: # 如果雙引號的數量為偶數,則說明“//”是注釋標志    # print self.instr[:cmtPos]    return self.instr[:cmtPos]   rearLine = rearLine[slashPos+2:]   # print rearLine   cmtPos += 2  # print self.instr  return self.instr # 判斷是否為轉義字符 def isEscapeOpr(self, instr):  if len(instr) <= 0:   return False  cnt = 0  while instr[-1] == '//':   cnt += 1   instr = instr[:-1]  if cnt % 2 == 1:   return True  else:   return False# 從json文件的路徑JsonPath讀取該文件,返回json對象def loadJson(JsonPath): try:  srcJson = open(JsonPath, 'r') except:  print CAUTION_PRINT_HEAD + 'cannot open ' + JsonPath  quit() dstJsonStr = '' for line in srcJson.readlines():  if not re.match(r'/s*//', line) and not re.match(r'/s*/n', line):   xline = xstr(line)   dstJsonStr += xline.rmCmt() # print dstJsonStr dstJson = {} try:  dstJson = json.loads(dstJsonStr)  return dstJson except:  print CAUTION_PRINT_HEAD + JsonPath + ' is not a valid json file'  quit()# 帶縮進地在屏幕輸出json字符串def printRes(resStr): resStr = resStr.replace(',', ',/n') resStr = resStr.replace('{', '{/n') resStr = resStr.replace(':{', ':/n{') resStr = resStr.replace('}', '/n}') resStr = resStr.replace('[', '/n[/n') resStr = resStr.replace(']', '/n]') resStr = resStr resArray = resStr.split('/n') preBlank = '' for line in resArray:  if len(line) == 0:   continue  lastChar = line[len(line)-1]  lastTwoChars = line[len(line)-2:]  if lastChar in {'}', ']'} or lastTwoChars in {'},', '],'}:   preBlank = preBlank[:len(preBlank)-2]  try:   print preBlank + line.decode('utf-8')  except:   print(preBlank + '[%This line cannot be decoded%]')  if lastChar == '{' or lastChar == '[':   preBlank += ' '*2            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 祁东县| 兖州市| 沾化县| 隆昌县| 仁怀市| 同德县| 波密县| 安徽省| 丘北县| 天祝| 高州市| 静海县| 淮安市| 郑州市| 奈曼旗| 清徐县| 东乡| 丽江市| 遵化市| 乐东| 泗阳县| 石景山区| 军事| 北安市| 迭部县| 秦皇岛市| 读书| 东城区| 古交市| 濉溪县| 祁东县| 隆回县| 扎囊县| 中西区| 马尔康县| 云南省| 怀远县| 霍城县| 高阳县| 奉化市| 天津市|