简体   繁体   English

如何使用python的readline在行中使用列

[英]How to use columns in rows using readline of python

I have a big data, and I am trying to do some analysis on specific columns of it.我有一个大数据,我正在尝试对它的特定列进行一些分析。 I know the number of rows (around 2000), but the number of columns of it can be varied for each row.我知道行数(大约 2000 行),但每行的列数可以不同。 for opening the file and do analysis on it, I used:为了打开文件并对其进行分析,我使用了:

a=np.loadtxt("file.csv")

And I got this error:我收到了这个错误:

ValueError: Wrong number of columns at line 2 ValueError:第 2 行的列数错误

It happens because of having different columns.这是因为有不同的列。

So I tried to read the file using readline().所以我尝试使用 readline() 读取文件。 The point is that in readline, I do not know how to change the string into integer in a way that I can use columns.关键是在 readline 中,我不知道如何以可以使用列的方式将字符串更改为整数。 one example of the file is like this:该文件的一个示例是这样的:

0 12 123 53.884 -20
156 1234 1 -123 543 4924 0 -0.3

The numbers are separated by space, the number of columns are varied.数字以空格分隔,列数不同。

You can read row as string.您可以将行读取为字符串。 Then split row by blank.然后按空白分割行。

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

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