简体   繁体   English

Pandas读取txt文件的问题

[英]Problems with Pandas reading txt file

I am having a rough time getting my code (python 3) to read a txt file.我很难让我的代码(python 3)读取一个 txt 文件。 I am using Pandas to get it to work and I have it read the file and gets the right number of rows, but the module reads the file as one column and makes the entire dataframe into one column 0. Here is an example of the code.我正在使用 Pandas 让它工作,我让它读取文件并获取正确的行数,但是模块将文件读取为一列并将整个 dataframe 变为一列 0。这是代码示例.

import pandas as pd
import numpy as np


data = pd.read_csv(r'file.txt',header=None)

I have used the delimiters/seperaters setup too in the line of code like \t or ' ' but it couldn't read the file then.我在 \t 或 ' ' 之类的代码行中也使用了分隔符/分隔符设置,但当时它无法读取文件。 Here is an example of what the file looks like.这是文件外观的示例。

  JK+0923  7.05  19.3 200.4 -56.1   0.140   0.022 2010 GHT-Jermi

As you can see, there is no header.如您所见,没有 header。 Either way, would like help.无论哪种方式,都希望得到帮助。 Thanks.谢谢。 I want it to read the columns correctly.我希望它正确读取列。

import pandas as pd
import numpy as np


data = pd.read_csv(r'asd.txt',header=None,sep='\t')

This should work if thedelimiter in your case is tab如果您的情况下的分隔符是制表符,这应该可以工作

or you can use a regex like \s+ for the value of sep for accepting multiple spaces as delimiter或者您可以使用像 \s+ 这样的正则表达式作为 sep 的值来接受多个空格作为分隔符

The pd.read_csv() function expects a header when used in the standard way.当以标准方式使用时, pd.read_csv() function 期望 header 。 However, you can specify the header=None parameter, see this question for more details:但是,您可以指定header=None参数,有关详细信息,请参阅此问题:

Pandas read in table without headers Pandas 在没有标题的表中读取

As you pointed out in your question, you have already tried to specify the delimiter when reading in the file, so the combination of both should help you read the file in correctly:正如您在问题中指出的那样,您已经尝试在读取文件时指定分隔符,因此两者的组合应该可以帮助您正确读取文件:

data = pd.read_csv(r'file.txt',header=None, sep='\t')

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

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