這是我的代碼:爲什麼to_csv給出錯誤?
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是因爲我必須寫多個數據幀到同一個文件,我用count來檢查它是否第一次寫入
請提供完整的回溯。 – albert
你在使用熊貓嗎?你能否展示完整的追蹤 - 最好挑選一個短語 –