[英]Read specific column value from a text file using Python
I have a text file as following: 我有一个文本文件,如下所示:
1 1 2 1 1e8
2 1 2 3 1e5
3 2 3 2 2000
4 2 5 6 1000
5 2 4 3 1e4
6 3 6 4 5000
7 3 5 2 2000
8 3 2 3 5000
9 3 4 5 1e9
10 3 2 3 1e6
In my text (which is very larger than this example) the second column is number of layer and the last one is energy in that layer, I want to extract the energy in each layer, For example for the number 2 in the second column, I need energy related to this Layer from the last column, and I want to separate this part of text file 在我的文字中(比这个示例大得多),第二列是层数,最后一列是该层中的能量,我想提取每一层中的能量,例如第二列中的数字2,我需要从上一列中获得与该图层有关的能量,并且我想将文本文件的这一部分分开
3 2 3 2 2000
4 2 5 6 1000
5 2 4 3 1e4
How can I do this work in python? 如何在python中完成这项工作?
You can grab the layers and energies from the text file like this 您可以像这样从文本文件中获取层次和能量
layers = []
energies = []
with open(file) as f:
for line in f:
linesplit = line.strip().split() # splits by whitespace
layers.append(int(linesplit[1])) # 2nd index
energies.append(float(linesplit[-1])) # last index
Edit: if you have a header line (at say, line 1) you can skip it with: 编辑:如果您有标题行(例如,第1行),则可以使用以下命令跳过它:
header_line = 1 # or whatever it is
with open(file) as f:
for line_number, line in enumerate(f, 1):
if line_number <= header_line:
continue
linesplit = line.strip().split()
layers.append(int(linesplit[1]))
energies.append(float(linesplit[-1]))
I don't know what your file looks like because you haven't posted the full thing so I can't help you more than this without seeing the whole thing (ie on pastebin.com). 我不知道您的文件是什么样子,因为您还没有发布完整的内容,因此在看不到整个内容的情况下(即在pastebin.com上),我将为您提供更多帮助。
One last try: 最后尝试:
layers = []
energies = []
with open(file) as f:
for lineno, line in enumerate(f, 1):
linesplit = line.strip().split() # splits by whitespace
if not linesplit: # empty
continue
try:
layers.append(int(linesplit[1])) # 2nd inde
except (TypeError, IndexError):
print("Skipping line {}: {!r}".format(lineno, line))
continue
try:
energies.append(float(linesplit[-1])) # last index
except TypeError:
layers.pop()
print("Skipping and reverting line {}: {!r}".format(lineno, line)):
Why don't you create a CSV file in the first place? 为什么不首先创建CSV文件? So you can seperate each value/column with ';'. 因此,您可以使用“;”分隔每个值/列。 Every new row, you print a new line in that CSV file. 在每一行中,您都会在该CSV文件中打印新行。
If it is a CSV you can simply use 'split' 如果是CSV,则只需使用“拆分”
line.split(';')[column you want]
example: 例:
line = '1;1;2;1;1e8'
print(line.split(';')[5])
>> 1e8
EDIT: read all lines from a file and put it in an array. 编辑:从文件中读取所有行并将其放入数组中。 NOTE: this code is not tested and was written quickly. 注意:此代码未经测试,并且编写迅速。 It should show direction you have to go. 它应该显示您必须走的方向。
elements = []
f.open('filename')
lines = f.readlines()
for x, line in lines:
elemenets.append([])
for y in range(0,5):
elements[x].append(line.split()[y])
If you already know what line you need, you can simply use: 如果您已经知道需要什么行,则可以使用:
f.open('filename')
lines = f.readlines()
print(lines[index_of_line].split()[index_of_item])
Split method without any argument will split string on whitespaces. 不带任何参数的split方法将在空白处分割字符串。 a.txt - is data filename. a.txt-是数据文件名。
#!/usr/bin/env python
with open ('a.txt') as f:
for line in f:
line.strip() # Removes \n and spaces on the end
var1, var2, var3, var4, var5 = line.split()
print(var1, var2, var3, var4, var5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.