[英]Reading specific columns from a text file in python
我有一个文本文件,其中包含一个由数字组成的表格,例如:
5 10 6
6 20 1
7 30 4
8 40 3
9 23 1
4 13 6
例如,如果我想要只包含在第二列中的数字,我如何将该列提取到列表中?
f=open(file,"r")
lines=f.readlines()
result=[]
for x in lines:
result.append(x.split(' ')[1])
f.close()
你可以使用列表理解来做同样的事情
print([x.split(' ')[1] for x in open(file).readlines()])
关于split()
文档
string.split(s[, sep[, maxsplit]])
返回字符串
s
的单词列表。 如果可选的第二个参数 sep 不存在或 None ,则单词由任意的空白字符字符串(空格、制表符、换行符、返回、换页符)分隔。 如果第二个参数 sep 存在而不是 None,则它指定一个字符串作为单词分隔符。 返回的列表将比字符串中分隔符的非重叠出现次数多一项。
所以,你可以省略我使用的空间,只做x.split()
但这也会删除制表符和换行符,请注意这一点。
您有一个空格分隔文件,因此请使用专为读取分隔值文件而设计的模块csv
。
import csv
with open('path/to/file.txt') as inf:
reader = csv.reader(inf, delimiter=" ")
second_col = list(zip(*reader))[1]
# In Python2, you can omit the `list(...)` cast
zip(*iterable)
模式可用于将行转换为列,反之亦然。 如果您正在逐行阅读文件...
>>> testdata = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
>>> for line in testdata:
... print(line)
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
...但需要列,您可以将每一行传递给zip
函数
>>> testdata_columns = zip(*testdata)
# this is equivalent to zip([1,2,3], [4,5,6], [7,8,9])
>>> for line in testdata_columns:
... print(line)
[1, 4, 7]
[2, 5, 8]
[3, 6, 9]
我知道这是一个老问题,但没有人提到当你的数据看起来像一个数组时,numpy 的loadtxt会派上用场:
>>> import numpy as np
>>> np.loadtxt("myfile.txt")[:, 1]
array([10., 20., 30., 40., 23., 13.])
您可以使用带有列表理解的zip
函数:
with open('ex.txt') as f:
print zip(*[line.split() for line in f])[1]
结果 :
('10', '20', '30', '40', '23', '13')
首先,我们打开文件并作为datafile
然后我们应用.read()
方法读取文件内容,然后我们拆分数据,返回如下内容: ['5', '10', '6', '6', '20', '1', '7', '30', '4', '8', '40', '3', '9', '23', '1', '4', '13', '6']
并且我们在这个列表上应用了列表切片,从索引位置 1 的元素开始并跳过接下来的 3 个元素,直到它到达循环的末尾。
with open("sample.txt", "r") as datafile:
print datafile.read().split()[1::3]
输出:
['10', '20', '30', '40', '23', '13']
它可能有帮助:
import csv
with open('csv_file','r') as f:
# Printing Specific Part of CSV_file
# Printing last line of second column
lines = list(csv.reader(f, delimiter = ' ', skipinitialspace = True))
print(lines[-1][1])
# For printing a range of rows except 10 last rows of second column
for i in range(len(lines)-10):
print(lines[i][1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.