简体   繁体   English

从文件中读取第一行是字符串的列

[英]Read columns from file where first row is string

What I would like to do is to read in columns from a .dat file. 我想做的是从.dat文件中读取列。 I have been able to do this using scitools.filetable.read_columns() . 我已经能够使用scitools.filetable.read_columns()做到这一点。 The problem that I am having is that the first row of my .dat file contains strings . 我遇到的问题是.dat文件的第一行包含strings How can I skip the first row? 如何跳过第一行?

So for a short example I have the following .dat file: 因此,作为一个简短示例,我具有以下.dat文件:

a   b   c   d   e
1   3   5   7   9
2   4   6   8  10

From this type of .dat file I want to create arrays for each column without the string . 从这种类型的.dat文件中,我想为每个没有string列创建数组。 When the .dat file would not contain a,b,c,d,e it would be very easy as it would just be: 当.dat文件不包含a,b,c,d,e ,它将非常简单,因为它只是:

import scitools.filetable

fp = open("blabla.dat", "r")

a, b, c, d, e = scitools.filetable.read_columns(fp)

Now a would read: [1 2] . 现在a将显示为: [1 2]

However, when I try to do the same thing when a , b , c , d , and e are part of the .dat file, as indicated in my example, scitools does not work since it cannot convert a string to a float . 但是,如我的示例所示,当abcde.dat文件的一部分时,当我尝试做同样的事情时, scitools不起作用,因为它无法将string转换为float How can I open the file without the first row or create the desired columns? 如何在没有第一行的情况下打开文件或创建所需的列?

Use fp.next to advance one line further 使用fp.next进一步前进一行

As fp is file descriptor for file open in text mode, iterating over it reads it line by line. 由于fp是在文本模式下打开文件的文件描述符,因此对其进行迭代读取会逐行读取它。

You can ask fp to read one line further by fp.next() and then pass it to your `scitools.filetable.read_colums(fp) 您可以要求fp通过fp.next()进一步读一行,然后将其传递给您的`scitools.filetable.read_colums(fp)

Your code would look like this after modification: 修改后的代码如下所示:

import scitools.filetable

fp = open("blabla.dat", "r")
fp.next()

a, b, c, d, e = scitools.filetable.read_columns(fp)

Or using context manager for closing the file: 或使用上下文管理器关闭文件:

import scitools.filetable

with open("blabla.dat", "r") as fp:
    fp.next()

    a, b, c, d, e = scitools.filetable.read_columns(fp)

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

相关问题 如何过滤第一行(不是标题)以字符串开头的列 - How to filter for columns where the first row (not header) starts with string 如何从csv文件读取前两列 - how to read first two columns from csv file 如何读取缺少列的 csv 文件并删除每行的第一个和最后一个分隔符? - how to read csv file with missing columns and remove first and last delimiter of each row? 来自 Pandas 的 read_excel 未读取所有数据(第一行缺少列) - read_excel from Pandas not reading all data (missing columns from first row) 如何将 csv 文件读入熊猫,跳过行直到某个字符串,然后选择第一行作为标题和分隔符作为 | - How to read csv file into pandas, skipping rows until a certain string, then selecting first row after as header and delimiter as | 将第一行包含一个的那些列设置零 - Set zeros to those columns where first row contains ones 读取包含字符串和数字列的txt文件 - Read txt file with string and number columns 读取文本文件中的行并将其转换为字符串列表 - Read and convert row in text file into list of string CSV文件的第一行是一个字符串的列表,而其余行是Python中的元素列表 - First row from a CSV file is a list of one string while the remaining rows are list of elements in Python 如何读取列由双引号和空格分隔的 csv 文件 - How to read csv file where columns are separated by double quotes and spaces
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM