简体   繁体   English

使用Python 2.7读取文本文件中特定行和列的数据

[英]Reading a specific row & columns of data in a text file using Python 2.7

I am new to Python and need to extract data from a text file. 我是Python的新手,需要从文本文件中提取数据。 I have a text file below: 我在下面有一个文本文件:

UNHOLTZ-DICKIE CORPORATION
CALIBRATION DATA
789     3456
222     455
333     5
344     67788
12      6789
2456    56656

And I want to read it on the shell as two columns of data only: 我只想将其作为两列数据在外壳上读取:

789     3456
222     455
333     5
344     67788
12      6789
2456    56656

Here's a Python program that reads a file and outputs the 3rd... lines (drops the first 2 lines). 这是一个Python程序,该程序读取文件并输出第3条...行(删除前2行)。 That's all I can deduce that you want given your short explanation. 简短的说明,这就是我所能推断的。

# read the whole file
file = open("input.file", 'r')
lines = file.readlines()
file.close()

# Skip first 2 lines, output the rest to stdout
count = 0
for line in lines:
    count +=1
    if count > 2:
         print line,

If you have numpy installed then this is a one-liner: 如果您安装了numpy,则这是一个单行代码:

   col1,col2 = numpy.genfromtxt("myfile.txt",skiprows=2,unpack=True)

where myfile.txt is your data file. 其中myfile.txt是您的数据文件。

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

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