我想打開並解析使用python腳本的Json文件,並在格式化後將其內容寫入另一個Json文件中,如我所願。現在我的源Json文件有字符/「 我想替換爲空白。我在解析或創建新聞文件時沒有任何問題,只是問題是該字符沒有被替換爲空白。做我做的早些時候,我已經實現了同樣的任務,但再有就是在文檔中沒有這樣的字符時Unicode編碼的一些問題
這裏是我的代碼
doubleQuote = "\""
try:
destination = open("TodaysHtScrapedItemsOutput.json","w") # open JSON file for output
except IOError:
pass
with open('TodaysHtScrapedItems.json') as f: #load json file
data = json.load(f)
print "file successfully loaded"
for dataobj in data:
for news in data[cnt]["body"]:
news = news.encode("utf-8")
if(news.find(doubleQuote) != -1): # if doublequotes found in first body tag
# print "found double quote"
news.replace(doubleQuote,"")
if(news !=""):
my_news = my_news +" "+ news
destination.write("{\"body\":"+ "\""+my_news+"\"}"+"\n")
my_news = ""
cnt= cnt + 1
並且解決了我的問題,但之前在其他程序中,我只是用來調用string.replace並使用它工作......現在我完全困惑......無論如何解決了我的問題...... thnx –