我尝试使用
将pandas数据帧转换为csv<body>
<main>
<div class="scene">
<div class="click-counter">0</div>
<div class="animated-element">
<span>ABC</span>
</div>
<div class="button">
Press me several times
</div>
</div>
<ul class="logger"></ul>
</main>
</body>
然后我得到了包含5列的csv文件,第一列是文本,我打开了csv文件,发现有些行以第一列的引号开头和结尾,而其他行则没有(如下所示)。我希望他们所有人都以引号开头和结尾,我该怎么办?非常感谢。
“Pt因胸痛而来到急诊室..... PT / INR和PTT在0324检查, PT / INR为92.5 / 8 .8和PTT wa 249。“,管理/监视,notprescribe /顺序,管理/监视,notadr
“病人在2015年4月5日在办公室看到....... Thi wa抓住了 另一位照顾者并向我的诊所报告 经理“,录制/准备/分配,notprescribe /顺序,notadminister /监视器,notadr
不同提供者在Med Rec上输入多种药物 不准确的剂量和......医院 入院时,开/订单,规定/顺序,notadminister /监视器,notadr
答案 0 :(得分:2)
您需要将quoting=True
添加到dataframe.to_csv
,如下所示:
dataframe.to_csv('final_processed.csv', encoding='utf-8', index=False, quoting=True)
您可以在官方文档here中找到更多详细信息。
这是一个完全有效的例子
import pandas as pd
from StringIO import StringIO
st = """
"Pt come to ER with chest pain ..... PT/INR and PTT be check at 0324, PT/INR as 92 .5/8 .8 and PTT wa 249.",administer/monitor,notprescribe/order,administer/monitor,notadr
"Patient wa see in office on 05/04/2015 ....... Thi wa catch by another caregiver and report to my clinic manager.",transcribe/prepare/dispense,notprescribe/order,notadminister/monitor,notadr
Multiple medication enter on Med Rec by different provider with inaccurate dose and ......hospital admission,prescribe/order,prescribe/order,notadminister/monitor,notadr
"""
data =pd.read_csv(StringIO(st), delimiter=",", header=None)
data.to_csv("final_processed.csv", encoding="utf-8", index=False, quoting=True)
输出如下:
"0","1","2","3","4"
"Pt come to ER with chest pain ..... PT/INR and PTT be check at 0324, PT/INR as 92 .5/8 .8 and PTT wa 249.","administer/monitor","notprescribe/order","administer/monitor","notadr"
"Patient wa see in office on 05/04/2015 ....... Thi wa catch by another caregiver and report to my clinic manager.","transcribe/prepare/dispense","notprescribe/order","notadminister/monitor","notadr"
"Multiple medication enter on Med Rec by different provider with inaccurate dose and ......hospital admission","prescribe/order","prescribe/order","notadminister/monitor","notadr"