[英]How to read file with space separated values in pandas
I try to read the file into pandas. The file has values separated by space, but with different number of spaces I tried:我尝试将文件读入 pandas。该文件的值由空格分隔,但我尝试使用不同数量的空格:
pd.read_csv('file.csv', delimiter=' ')
but it doesn't work但它不起作用
添加delim_whitespace=True
参数,它比正则表达式快。
您可以使用正则表达式作为分隔符:
pd.read_csv("whitespace.csv", header=None, delimiter=r"\s+")
If you can't get text parsing to work using the accepted answer (eg if your text file contains non uniform rows) then it's worth trying with Python's csv library - here's an example using a user defined Dialect :如果您无法使用接受的答案进行文本解析(例如,如果您的文本文件包含非统一行),那么值得尝试使用 Python 的 csv 库 - 这是使用用户定义的方言的示例:
import csv
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)
Pandas read_fwf for the win: Pandas read_fwf 获胜:
import pandas as pd
df = pd.read_fwf(file_path)
You can pass a regular expression as a delimiter for read_table also, and it is fast:).您也可以传递一个正则表达式作为 read_table 的分隔符,而且速度很快:)。
result = pd.read_table('file', sep='\s+')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.