Python 如何在熊猫中读取带有空格分隔值的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19632075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read file with space separated values in pandas
提问by yemu
I try to read the file into pandas. The file has values separated by space, but with different number of spaces I tried:
我尝试将文件读入熊猫。该文件具有由空格分隔的值,但我尝试了不同数量的空格:
pd.read_csv('file.csv', delimiter=' ')
but it doesn't work
但它不起作用
采纳答案by HYRY
add delim_whitespace=True
argument, it's faster than regex.
添加delim_whitespace=True
参数,它比正则表达式快。
回答by yemu
you can use regex as the delimiter:
您可以使用正则表达式作为分隔符:
pd.read_csv("whitespace.csv", header=None, delimiter=r"\s+")
回答by Pierz
The accepted answer doesn't appear to work with newer versions of Python so here's a more up to date example using a user defined Dialect:
接受的答案似乎不适用于较新版本的 Python,因此这里有一个使用用户定义的方言的更新示例:
csv.register_dialect('skip_space', skipinitialspace=True)
with open(my_file, 'r') as f:
reader=csv.reader(f , delimiter=' ', dialect='skip_space')
for item in reader:
print(item)