首页 > 解决方案 > 如何在 pandas 中使用 to_csv 函数时解决分隔符错误

问题描述

这是关于如何逐个单元格编写单行单元格并将其填充到 csv 文件中的后续问题

 import pandas as pd
 delim = "@@@"
 # just for abrevation
 tmp = pd.read_csv("mycsv.csv",sep='@@@')
 tmp.loc[0,:] = "None"
 tmp.to_csv("mycsv.csv",sep=delim,index=False)

我收到跟随错误。请提出一些正确的解决方案

self.writer = csv.writer(f, **writer_kwargs)
TypeError: "delimiter" must be a 1-character string
Exception ignored in: <module 'threading' from 
Traceback (most recent call last):
  line 1296, in _shutdown
 _main_thread._delete()
File /share/python-3.6.1/linux_x86_64/lib/python3.6/threading.py", line 1015, 
in _delete
del _active[get_ident()]
KeyError: 140613724829696

标签: pythonpandas

解决方案


根据Errorto_csv文档,它只支持一个字符。

read_csv支持str. 这就是为什么阅读和写作失败的原因。您可能需要将分隔符更改为一个字符。


推荐阅读