Converting file data to nested list
I have a string of numbers that I would like to convert to a nested list. So far, I have
with open('lifedata.txt') as f: table_data = [ line.split() for line in f] print(table_data)
If the text document consists of numbers ordered like this,
0000000 0010000 0001000 0111000 0000000 0000000
The code I have so far only creates a nested list that looks like, [['0000000'], ['0010000'], ['0001000'], ['0111000'], ['0000000'], ['0000000']]
But instead, I wanted it took be [[0,0,0,0,0,0,0],,] and so on. I also do not know how to convert the string into an integer. I'm just very confused on how I should manipulate the original text document to do what I want it to.
This is what is happening:
>>> "0000000".split() ['0000000']
Instead, call int() on every character in each string:
[[int(c) for c in line.strip()] for line in f]
Or, via map():
[list(map(int, line.strip())) for line in f]
Use this ,it will return a list of containing data from each line of txt.