解决错误“分隔符必须为1个字符的字符串".在将数据帧写入csv文件时 [英] Solving error "delimiter must be a 1-character string" while writing a dataframe to a csv file

查看:2687
本文介绍了解决错误“分隔符必须为1个字符的字符串".在将数据帧写入csv文件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此问题:熊猫将数据帧写入CSV文件作为模型,我编写了以下代码来制作一个csv文件:

Using this question: Pandas writing dataframe to CSV file as a model, I wrote the following code to make a csv file:

df.to_csv('/Users/Lab/Desktop/filteredwithheading.txt', sep='\s+', header=True)

但是它返回以下错误:

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

我在这里 http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html ,但我无法弄清我所缺少的内容或该错误的含义.我还尝试在代码中使用(sep ='\ s'),但出现了相同的错误.

I have looked up the documentation for this here http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html but I can't figure out what I am missing, or what that error means. I also tried using (sep='\s') in the code, but got the same error.

推荐答案

请注意,尽管此错误的解决方案是使用字符串字符而不是regex,但当将from __future__ import unicode_literals与有效的unicode字符一起使用时,pandas也会引发此错误.截至2015年11月16日发行的0.16.2版本,此错误仍然是熊猫中的已知错误:
"to_csv扼流圈,即使未将sep作为字符串传递,即使将编码设置为unicode也是如此#6035

Note that the although the solution to this error was using a string charcter instead of regex, pandas also raises this error when using from __future__ import unicode_literals with valid unicode characters. As of 2015-11-16, release 0.16.2, this error is still a known bug in pandas:
"to_csv chokes if not passed sep as a string, even when encoding is set to unicode" #6035

例如,其中df是熊猫DataFrame:

For example, where df is a pandas DataFrame:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import pandas as pd

df.to_csv(pdb_seq_fp, sep='\t', encoding='utf-8')

TypeError:定界符"必须为1个字符的字符串

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

使用字节的文字使用指定的编码(Python 3使用默认utf-8)-*- coding: utf-8 -*-将在熊猫0.16.2中解决此问题:(b'\t')—我尚未在以前的版本或0.17.0中进行过测试.

Using a byte lteral with the specified encoding (default utf-8 with Python 3) -*- coding: utf-8 -*- will resolve this in pandas 0.16.2: (b'\t') —I haven't tested with previous versions or 0.17.0.

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import pandas as pd

df.to_csv(pdb_seq_fp, sep=b'\t', encoding='utf-8')

(请注意,在版本0.13.0-???中,必须使用pandas.compat import u;但在0.16.2之前,字节字面量才是可行的方法.)

(Note that with versions 0.13.0 - ???, it was necessary to use pandas.compat import u; but by 0.16.2 the byte literal is the way to go.)

这篇关于解决错误“分隔符必须为1个字符的字符串".在将数据帧写入csv文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆