reading variable number of columns with Python -


i need read variable number of columns input file ( number of columns defined user, there's no limitation ). every column have multiple variables read, 3 in case, set user well.

so file read like:

2   3   5 6   7   9 3   6   8 

in fortran easy do:

do 180 i=1,nmod read(10,*) qarr(i),rarr(i),warr(i) 

nmod defined user, values in example. of them input parameters stored in memory. doing these can save variables need , can use whenever want, recalling them changing index. how can obtain same result python?

example file 'text'

2 3 5 6 7 9 3 6 8 

python code

data = []  open('text') file:     columns_to_read = 1  # here tell how many columns want read per line     line in file:         data.append(list(map(int, line.split()[:columns_to_read])))  print(data)  # print: [[2], [6], [3]] 

data hold array of arrays represent lines.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -