简体   繁体   English

用 Pandas 加载 .dat 文件,制作两列,其中 '\\' 是分隔符

[英]Load .dat file with pandas, make two columns where '\' is separator

How to load .dat file in two columns with slash as separator with pandas library?如何在两列中加载.dat文件,斜线作为pandas库的分隔符?

0     NaN\t1.000000      NaN
1     0.00\t-3.0         NaN
2     0.04\t-4.9         NaN
3     0.08\t-4.47        NaN

With code:用代码:

df = pd.read_csv("dummy.dat", sep="\",skiprows = 0)

I get an error我收到一个错误

File "<ipython-input-7-8a49552b5bf1>", line 6 df = pd.read_csv("dummy.dat", sep="\", header=None, skiprows=0) ^ 

SyntaxError: EOL while scanning string literal语法错误:扫描字符串文字时 EOL

Look at your syntax highlighting, it is your friend:看看你的语法高亮,它是你的朋友:

df = pd.read_csv("dummy.dat", sep="\",skiprows = 0)

See how the right bracket after skiprows is not coloured the same was as the opening bracket?看到skiprows 后右括号的颜色与左括号的颜色不同吗? That's a hint that there is a problem in between.这暗示两者之间存在问题。

Using separator="\\" you have escaped the " rather than making a closing quote. Compare it with使用separator="\\"已经转义了"而不是做一个结束引号。将它与

df = pd.read_csv("dummy.dat", sep="\"",skiprows = 0)

In any case, your separator is \\t not \\ , for at least part of your data, so try无论如何,对于至少部分数据,您的分隔符是\\t而不是\\ ,所以请尝试

df = pd.read_csv("dummy.dat", sep='\t',skiprows = 0)

You should clean up your data before importing it, however.但是,您应该在导入之前清理数据。 It looks like you have a mix of tab characters (we can see between columns) and escaped tabs (which we see between columns 2 and 3, as in 0.00\\t-3.0 ).看起来您混合了制表符(我们可以在列之间看到)和转义制表符(我们在第 2 列和第 3 列之间看到,如0.00\\t-3.0 )。 If you convert those escaped \\t to actual tabs.如果您将那些转义的\\t转换为实际的制表符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM