0

line.read contains

25,26,27,28,29,30,31,32.33,34,35,36,37,38,39,40.25,26,27,28,29,30,31,32.33,34,35,36,37,38,39,40.25,26,27,28,29,30,31,32.33,34,35,36,37,38,39,40.25,26,27,28,29,30,31,32.33,34,35,36,37,38,39,40

My single line reader is here

output = open('line.read', 'r')  # See the r
nodes_list = output.read().strip().split('.')
mylist = [[int(cell) for cell in row.split(',')] for row in nodes_list]

My output is in this code.

['25,26,27,28,29,30,31,32', '33,34,35,36,37,38,39,40', '25,26,27,28,29,30,31,32', '33,34,35,36,37,38,39,40', '25,26,27,28,29,30,31,32', '33,34,35,36,37,38,39,40', '25,26,27,28,29,30,31,32', '33,34,35,36,37,38,39,40']

This code reads only one line however i want to read multiple lines and convert it to 8x8 integer array. Old value is not important for me. I just want to read current line. How can i do that ? It will read first line it will do smth after that it will look second line etc.

My txt will be something like this

25,26,27,28,29,30,31,32.33,34,35,36,37,38,39,40.25,26,27,28,29,30,31,32.33,34,35,36,37,38,39,40.25,26,27,28,29,30,31,32.33,34,35,36,37,38,39,40.25,26,27,28,29,30,31,32.33,34,35,36,37,38,39,40
22,33,21,33,40,37,24,23.15,13,12,35,33,12,15,23.22,33,21,33,40,37,24,23.15,13,12,35,33,12,15,23.22,33,21,33,40,37,24,23.15,13,12,35,33,12,15,23.22,33,21,33,40,37,24,23.15,13,12,35,33,12,15,23
...

I can find it by looking empty cell or i can put a special character to my txt file which can know end of the line. However, If i do this, it will read it as an string and also it shows all value. It should do that line by line. Thanks for helping

1
  • The output you are getting is already a 8x8 matrix, the one line is just the representation which you can change by printing it differently. Commented Sep 5, 2018 at 8:41

1 Answer 1

1

Although it is not abundantly clear to me, but I think this is what you are looking for.

with open('test.txt','r') as fout:
    line = fout.readline()
    while line:
        a = [[int(col) for col in row.split(',')] for row in line.split('.')]
        print(a)
        #Do something with your list a
        line = fout.readline()

Output for the print() function:

[[25, 26, 27, 28, 29, 30, 31, 32], [33, 34, 35, 36, 37, 38, 39, 40], [25, 26, 27, 28, 29, 30, 31, 32], [33, 34, 35, 36, 37, 38, 39, 40], [25, 26, 27, 28, 29, 30, 31, 32], [33, 34, 35, 36, 37, 38, 39, 40], [25, 26, 27, 28, 29, 30, 31, 32], [33, 34, 35, 36, 37, 38, 39, 40]]
[[22, 33, 21, 33, 40, 37, 24, 23], [15, 13, 12, 35, 33, 12, 15, 23], [22, 33, 21, 33, 40, 37, 24, 23], [15, 13, 12, 35, 33, 12, 15, 23], [22, 33, 21, 33, 40, 37, 24, 23], [15, 13, 12, 35, 33, 12, 15, 23], [22, 33, 21, 33, 40, 37, 24, 23], [15, 13, 12, 35, 33, 12, 15, 23]]

And last but not least, there's no array in the core Python library. Always say List when you mean array.

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

Comments

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.