have_df = pd.DataFrame({'User':['101','101','101'],'json_text':["""{"president":{"name": "Zaphod Beeblebrox","species": "Betelgeusian"}}""","""{"president":{"name": "Zaphod Beeblebrox","species": "Betelgeusian"}}""",'blank']})
我想要这个导出管道分隔的文件并尝试了以下操作:
have_df.to_csv('have_df.csv',sep="|")
当我打开并看到管道文件时,json文本值周围有一个额外的双引号,如下所示:
"{""president"":{""name"": ""Zaphod Beeblebrox"",""species"": ""Betelgeusian""}}"
如何删除这种额外的双引号编程方式?谢谢
最佳答案
首先评估JSON数据,然后保存到csv:
import json
(have_df.json_text
.replace('blank', "None")
.apply(ast.literal_eval)
.to_csv('file.csv', sep='|')
)
file.csv
0|{'president': {'name': 'Zaphod Beeblebrox', 'species': 'Betelgeusian'}}
1|{'president': {'name': 'Zaphod Beeblebrox', 'species': 'Betelgeusian'}}
2|