简体   繁体   中英

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

3  2  3  2  2000
4  2  5  6  1000
5  2  4  3  1e4 

How can I do this work in 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:

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).

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? So you can seperate each value/column with ';'. Every new row, you print a new line in that CSV file.

If it is a CSV you can simply use 'split'

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. a.txt - is data filename.

#!/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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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