Python Pandas - use Multiple Character Delimiter when writing to_csv

∥☆過路亽.° 提交于 2021-02-20 18:46:10

问题


It appears that the pandas to_csv function only allows single character delimiters/separators.

Is there some way to allow for a string of characters to be used like, "::" or "%%" instead?

I tried:

df.to_csv(local_file,  sep = '::', header=None, index=False)

and getting:

TypeError: "delimiter" must be a 1-character string

回答1:


Use numpy-savetxt

Ex:

np.savetxt(file.csv, np.char.decode(chunk_data.values.astype(np.bytes_), 'UTF-8'), delimiter='~|', fmt='%s',encoding=None)

np.savetxt(file.dat, chunk_data.values, delimiter='~|', fmt='%s',encoding='utf-8')



回答2:


Think about what this line a::b::c‘ means to a standard CSV tool: an a, an empty column, a b, an empty column, and a c. Even in a more complicated case with quoting or escaping:"abc::def"::2 means an abc::def, an empty column, and a 2.

So, all you have to do is add an empty column between every column, and then use : as a delimiter, and the output will be almost what you want.

I say “almost” because Pandas is going to quote or escape single colons. Depending on the dialect options you’re using, and the tool you’re trying to interact with, this may or may not be a problem. Unnecessary quoting usually isn’t a problem (unless you ask for QUOTE_ALL, because then your columns will be separated by :"":, so hopefully you don’t need that dialect option), but unnecessary escapes might be (e.g., you might end up with every single : in a string turned into a \: or something). So you have to be careful with the options. But it’ll work for the basic “quote as needed, with mostly standard other options” settings.



来源:https://stackoverflow.com/questions/51328454/python-pandas-use-multiple-character-delimiter-when-writing-to-csv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!