我有一個由twitter刮取的json。一些推文在使用報價之前有反斜槓。這隻在推特消息中看到,而不是我的JSON的密鑰。 我有下面的代碼,這將刪除大量的垃圾,包括反斜槓,但新保存的json仍然有背面削減python3:pandas'to_json將添加到我的一個鍵值中的每個雙引號
我最近發現問題不是正則表達式,但與to_json。熊貓通過正則表達式去除反斜槓,然後在使用to_json時重新應用反斜槓。有什麼方法可以解決這個問題嗎?我的目標是讀取原始格式爲JSON,刪除不必要的混亂,然後將其重新保存到一個新的JSON在它進來直接從全新的JSON(在記事本中打開
import pandas as pd
data = pd.DataFrame(pd.read_json(filename, orient=columnName,encoding="utf-8"),columns=columnName)
data['full_text'] = data['full_text'].replace('^@ABC(\\u2019s)*[ ,\n]*', '', regex=True)
data['full_text'] = data['full_text'].replace('(\\u2019)', "'", regex=True)
data['full_text'] = data['full_text'].apply(lambda x: re.sub(r'\\(?=\")','"',x))
print(data['full_text'].loc[data['id_str'] == 79104606646138400])
data.to_json('new_abc_short.json',orient='records')
例如鍵值對完全一樣的格式++ ):
....,"full_text":"How can you \"accidentally close\" my account",....
我的預期:
....,"full_text":"How can you "accidentally close" my account",....
不是每一個雙引號有問題,想在關鍵full_text,我擔心的部分是它的價值。它很重要,我從打印中注意到full_text是dtype對象
aw :(好吧然後,我想這很好 – user3120554