diedro
diedro

Reputation: 613

python reading file with different informations according to line

I have a strange file:

515   30.00398       30.00153
1  4
A B A
A B B
A B C
A B D
2  4
A C A
A C B
A C C
A C D

This has been creating according to "fortran logic" (unfortunately). I have to read it in python. For me it is difficult to move to python logic. Indeed I have to read the first line and to store it in some variables. The 2nd and 7th lines give me information about other variables which value are related to the "letters". I try to make myself clear.

I have variable called "cells". Each cell has number, in this case "1" and "2". Each cell has 4*3 elemenets. In this example cell "1" has:

A B A
A B B
A B C
A B D

how can I read all the file.

I have learnt to use:

for line in Lines:
    count += 1
    print("Line{}: {}".format(count, line.strip()))

This seems to work for files where all lines have the same type of element. What about my file. How can read it properly? Some suggestions?

Thanks in advance for any kind of help

Upvotes: 0

Views: 129

Answers (3)

Max Shouman
Max Shouman

Reputation: 1331

Since I don't know your desired output, I'm assuming a 3x4 matrix could be a good data structure for your cell.

Maybe building a dictionary with cell_number: matrix pairs could be a good implementation:

data = open("file.txt").readlines()[1:] # get a list of all lines except header

cells = {f"cell_{i+1}": data[i:i+4] for i in range(0, len(data), 4)}

This seems complicated as it's very comprehensive but it's kind of intuitive actually.

Mind you, this is a specific solution to a specific problem, so make sure what your file has the same structure as in your post.

Upvotes: 0

Epsi95
Epsi95

Reputation: 9047

# assuming that your file.txt is like

# 515   30.00398       30.00153
# 1  5
# A B A
# A B B
# A B C
# A B D
# P Q R
# 2  4
# A C A
# A C B
# A C C
# A C D

cells = [] # cell0, cell 1, cell2 etc

with open('file.txt') as f:
    # discarding 515   30.00398       30.00153
    next(f)
    
    cell_line = f.readline()
    while(cell_line != ''):
        cell_no, number_of_rows = map(int, cell_line.split())
        cell = [[0,0,0] for i in range(number_of_rows)]

        for i in range(number_of_rows):
            cell[i][0], cell[i][1], cell[i][2] = f.readline().split()
        cells.append(cell)

        cell_line = f.readline()
    
print(cells)

# [[['A', 'B', 'A'],
#   ['A', 'B', 'B'],
#   ['A', 'B', 'C'],
#   ['A', 'B', 'D'],
#   ['P', 'Q', 'R']], ---> cell 0
#  [['A', 'C', 'A'], ['A', 'C', 'B'], ['A', 'C', 'C'], ['A', 'C', 'D']]] ---> cell 1

Upvotes: 1

Ahmed Contrib
Ahmed Contrib

Reputation: 1066

Try using readLine #readLine() will read you line by line #readLines() will read entire file in a list

with open('fortan_file.txt') as file:
    lines = file.readLines()
    for count, line in enumerate(Lines):
      print("Line{}: {}".format(count, line.strip()))

if you are aware of lines in between

lines.seek(line_numer)

#seek will redirect the pointer to given line Adding a if condition would do the work!

Upvotes: 0

Related Questions