【发布时间】:2023-03-31 05:45:02
【问题描述】:
这是我的代码:
with open('myData', 'a') as f:
if count1 == 1:
df.to_csv(f,index=False, quoting=3 )
else:
df.to_csv(f,index=False, quoting=3 , header = False)
Error: need to escape, but no escapechar set
我该如何解决这个问题?我想我需要将引用更改为 None 并将引用字符更改为 '"'。我的方向是否正确?
这是完整的回溯:
---------------------------------------------------------------------------
Error Traceback (most recent call last)
<ipython-input-22-7b964e5d0ae8> in <module>()
27 action.perform()
28 html = browser.page_source
---> 29 ScrapePage(html)
<ipython-input-20-1d50d699fe76> in ScrapePage(html)
56 with open('myData', 'a') as f:
57 if count1 == 1:
---> 58 df.to_csv(f,index=False, quoting=3 )
59 else:
60 df.to_csv(f,index=False, quoting=3 , header = False)
C:\Anaconda2\lib\site-packages\pandas\core\frame.pyc in to_csv(self, path_or_buf, sep,
na_rep, float_format, columns, header, index, index_label, mode, encoding, compression,
quoting, quotechar, line_terminator, chunksize, tupleize_cols, date_format, doublequote,
escapechar, decimal, **kwds)
1330 escapechar=escapechar,
1331 decimal=decimal)
-> 1332 formatter.save()
1333
1334 if path_or_buf is None:
C:\Anaconda2\lib\site-packages\pandas\core\format.pyc in save(self)
1504
1505 else:
-> 1506 self._save()
1507
1508 finally:
C:\Anaconda2\lib\site-packages\pandas\core\format.pyc in _save(self)
1604 break
1605
-> 1606 self._save_chunk(start_i, end_i)
1607
1608 def _save_chunk(self, start_i, end_i):
C:\Anaconda2\lib\site-packages\pandas\core\format.pyc in _save_chunk(self, start_i, end_i)
1631 quoting=self.quoting)
1632
-> 1633 lib.write_csv_rows(self.data, ix, self.nlevels, self.cols, self.writer)
1634
1635 # from collections import namedtuple
pandas\lib.pyx in pandas.lib.write_csv_rows (pandas\lib.c:19840)()
Error: need to escape, but no escapechar set
在if: else: 中写入 csv 的原因是因为我必须将多个数据帧写入同一个文件。我正在使用计数来检查它是否是第一次写入。
【问题讨论】:
-
请提供完整的追溯。
-
你在用熊猫吗?你能显示完整的回溯吗——这比挑选一个短语要好得多
标签: python python-2.7 pandas dataframe