0

I need to read a variable number of columns from my input file ( the number of columns is defined by the user, there's no limitation ). For every column I have multiple variables to read, three in my case, set by the user as well.

So the file to read is like:

2   3   5
6   7   9
3   6   8

In Fortran this is really easy to do:

DO 180 I=1,NMOD
READ(10,*) QARR(I),RARR(I),WARR(I)

NMOD is defined by the user, as well as all the values in the example. All of them are input parameters to be stored in memory. By doing these I can save all the variables I need and I can use it whenever I want, recalling them by changing the I index. How can I obtain the same result with Python?

2 Answers 2

1

Example file 'text'

2 3 5
6 7 9
3 6 8

Python code

data = []

with open('text') as file:
    columns_to_read = 1  # here you tell how many columns you want to read per line
    for line in file:
        data.append(list(map(int, line.split()[:columns_to_read])))

print(data)  # print: [[2], [6], [3]]

data will hold an array of arrays that represent your lines.

Sign up to request clarification or add additional context in comments.

5 Comments

Two questions: 1) How can I recall the variables I need ? If I need just some of them how can i select those? 2) I need the script to read a number of columns dependent on NMOD. I have also other stuff in the file and I don't want to read it all. Is it doable? Many thanks.
You can do: data[x][y], where x=number of the line and y=column. So, data[2][2] would retrieve the number 8, last line and last number in this case.
If you have other data in the file that needs to be ignored, you should update your question with a proper example of the file, so people will take this into consideration when providing the code.
The Fortran code reads a NMOD number of columns, maybe I wasn't clear enough, I'm sorry. It is possible to do that just by slightly changing your code?
@GiacomoM if my answer responds your question correctly, please, don't forget to mark as 'best answer'.
0
from itertools import islice
with open('file.txt', 'rt') as f:
    # default slice from row 0 until end with step 1
    # example islice(10, 20, 2) take only row 10,12,14,16,18
    dat = islice(f, 0, None, 1)  
    column = None # change column here, default to all

    # this keep the list value as string 
    # mylist = [i.split() for i in dat]
    # this keep the list value as int
    mylist = [[int(j) for j for i.split()[:column] for i in dat]

Code above construct 2d list

access with mylist[row][column]

Example - mylist[2][3] access row 2 column 3

Edit : improve code efficiency with @Guillaume @Javier suggestion

6 Comments

I would open the file as a text file, (explicitly specifying mode 'rt' on open()) then use readlines().
@Guillaume thanks, edit the answer to use readlines(), i was thinking of the newline but its redundant since the split() should already take care that. For the 'rt' mode, i think its fine since its default with twxt read mode
realines() adds nothing and raises used memory. Without that method call the same code is better.
ah yes true, iterating over f directly is better, it will read the file line by line anyway :)
@Javier you are correct since i don need whole files in memory, change my answer to reflect this
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.