简体   繁体   English

如何读取 pandas 中空格分隔值的文件

[英]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.

相关问题 如何使用空格分隔的非恒定值读取文件 - how to read a file with space separated values that are not constant 在熊猫中读取具有多个制表符和空格分隔值的txt文件 - read txt file with multiple tab & space separated values in pandas 如何将一个空格分隔的文件(包含一些需要的、格式错误的行)读入 Pandas? - How to read a space separated file, with some needed, misformatted rows, into pandas? 读取 pandas 中的空格分隔文本文件 - Read space separated text file in pandas 如何使用空格分隔值逐行读取文件? - How to read a file line by line with space separated values? 如何用搁置来驱动空格分隔的值文件 - How to drive space separated values file with shelve 当值有空格时,将空格分隔的文件转换为 Pandas - Space separated file to Pandas when values have spaces 如何使用正则表达式组读取cedict(用空格分隔的文件)? - How to read the cedict (a space separated file) with regex groups? 如何使用Python 3中的readlines读取由空格分隔的整数输入文件? - How to read an input file of integers separated by a space using readlines in Python 3? 如何在python中读取用空格分隔的充满json对象的文本文件 - How to read text file full of json objects separated with space in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM