I have a text file in which there are numbers in each line(only numbers). My color.txt
looks like:
3
3
5
1
5
1
5
When I read this to list using
f=open('D:\\Emmanu\\project-data\\color.txt',"r")
for line in f:
g_colour_list.append(line.strip('\n'))
print g_colour_list
the output is like
['3', '3', '5', '1', '5', '1', '5']
But I want it as:
[3,3,5,1,5,1,5]
How can I do that in the single line that isg_colour_list.append(line.strip('\n'))
?
just cast your strings into integers:
g_colour_list.append(int(line.strip('\n')))
Wrap a call to python's int
function which converts a digit string to a number around your line.strip()
call:
f=open('D:\\Emmanu\\project-data\\color.txt',"r")
for line in f:
g_colour_list.append(int(line.strip('\n')))
print g_colour_list
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With