Reputation: 53
I have the following code which is used to extract a substring from each line of text on a file and put in a list of strings.
distribution = []
with open("./Output_Files/datafile.txt", "r") as fileRead:
for line in fileRead:
distr_chop = line.split()[3:]
distribution.append(distr_chop)
Then I convert each string in the list to an integer:
distribution = [int(i) for i in distribution]
However, I get an error when printing the list.
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
Upvotes: 0
Views: 130
Reputation: 145
dist_chop is in this case a list of 4th through nth "words" of the line thus distribution is an array of arrays which is not what I imagine you wanted.
use distribution += distr_chop
if you actually wanted all the things after the 3rd or distribution.append(line.split())[3]
if you just wanted just one element from each line
Upvotes: 1
Reputation: 261
distr_chop
is a list.
If it is a single element list, you can do
...
distribution.append(distr_chop[0])
# then proceed to use the list comprehension to convert each element to an integer
Upvotes: 1