Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何处理熊猫中的多值线终止符_Python_Python 3.x_Pandas_Csv - Fatal编程技术网

Python 如何处理熊猫中的多值线终止符

Python 如何处理熊猫中的多值线终止符,python,python-3.x,pandas,csv,Python,Python 3.x,Pandas,Csv,我将\x02\n作为我试图解析的csv文件中的行终止符。但是,我不能在熊猫中使用两个字符,它只允许使用一个,例如: >>> data = pd.read_csv(file, sep="\x01", lineterminator="\x02") >>> data.loc[100].tolist() ['\n1475226000146', '1464606', 'Juvenile', '1', 'http://itunes.apple.com/artist/juv

我将
\x02\n
作为我试图解析的csv文件中的行终止符。但是,我不能在熊猫中使用两个字符,它只允许使用一个,例如:

>>> data = pd.read_csv(file, sep="\x01", lineterminator="\x02")
>>> data.loc[100].tolist()
['\n1475226000146', '1464606', 'Juvenile', '1', 'http://itunes.apple.com/artist/juvenile/id1464606?uo=5', '1']
或:


在这里我们可以看到,
\n
没有被正确地切断。使用上述分隔符读取pandas中csv文件的最佳方法是什么?

从v0.23开始,pandas不支持多字符行终止符。您的代码当前返回:

s = "this\x01is\x01test\x02\nthis\x01is\x01test2\x02"
df = pd.read_csv(
    pd.compat.StringIO(s), sep="\x01", lineterminator="\x02", header=None)

df
        0   1      2
0    this  is   test
1  \nthis  is  test2
您唯一的选择(到目前为止)是从第一列中删除前导空格。您可以使用
str.lstrip
执行此操作

df.iloc[:, 0] = df.iloc[:, 0].str.lstrip()
# Alternatively,
# df.iloc[:, 0] = [s.lstrip() for s in df.iloc[:, 0]]

df

      0   1      2
0  this  is   test
1  this  is  test2
如果您必须处理多个其他类型的行终止符的剥离(除了换行符),您可以传递一个字符串:

line_terminators = ['\n', ...]
df.iloc[:, 0] = df.iloc[:, 0].str.lstrip(''.join(line_terminators))

是否所有行都有“\x02\n”作为行终止符?@AndreiOdegov这是分隔符,而不是行终止符。是一样的吗?@coldspeed是的,在这个文件中。但我确实有其他文件使用不同的多值分隔符,所以我不想只执行find all/replace等操作,这会使事情复杂化。到目前为止,还没有任何真正的支持。谢谢你。对工作或实习中的任何工作感兴趣(我们的公司在LA地区),而不是空白<代码> LaStudio<代码>,我使用了Rixi终结符中的字符,所以类似于:<代码> DF.IOC[[,0 ]=DF.IOC[[,0 ] ]。STR.LaBand(''.No.In LoNi终止符[1:])< /代码> @ DavID54 2:您是来自Digital Digital?戴维的:?我真的很感谢你的邀请,但是我已经在即将到来的春天有了一份校园工作,毕业后我将加入谷歌。我目前不想面试任何职位,但如果情况发生变化,我一定会给你一个平@David542好的,那么你有一个线路终端的列表?我可以修改我的解决方案。
line_terminators = ['\n', ...]
df.iloc[:, 0] = df.iloc[:, 0].str.lstrip(''.join(line_terminators))