Hey. I'm pretty new in Python. I'm trying to read from my file all the content and put it in a string. My file looks:
1 string1 2 string2 10 string 3
and I want to obtain: ["1", "string1", "2", "string 2", "10", "string3"] using this:
def read(self): file = open("Repository/file.txt", 'r') f = file.readlines() f = f.split(" ") print f
but I get this: AttributeError: 'list' object has no attribute 'split'
Thanks!
Use f = file.read() and f = f.split() .
f = file.read()
f = f.split()
with open('numb.txt') as f: print [i.split() for i in f] #[['1', 'string1'], ['2', 'string2'], ['10', 'string3']]
A little mistake like this for 1 list.
with open('numb.txt') as f: print f.read().split() #['1', 'string1', '2', 'string2', '10', 'string3']