Reputation: 31
So I have a txt file that contains the following numbers:
10
5
6
2
3
4
1
9
34
22
5
There is only one number per row. I want to put all the numbers in a list and use only the read()
function. readline()
or readlines()
is not allowed.
This is what I've tried doing (note that I have to use a function like this):
def get_list1(text):
result=[]
for row in text:
result.append(row)
return result
with open("file.txt") as f:
n = f.read()
l=get_list1(n)
print(l)
This is the output:
['1', '0', '\n', '5', '\n', '6', '\n', '2', '\n', '3', '\n', '4', '\n', '1',
'\n', '9', '\n', '3', '4', '\n', '2', '2', '\n', '5']
As you can see it includes the \n
and splits the numbers into its digits.
I want an output of
['10','5','6','2','3','4','1','9','34','22','5']
Upvotes: 1
Views: 328
Reputation: 51643
The short way to do this is
def get_list1(text):
return text.split("\n")
with open("file.txt") as f:
get_list1(f.read())
or you can replace the function by something like
# takes care of empty lines as well and create actual integers in a list
l = list(map(int, (w.strip() for w in f.read().split() if w.strip())))
Yours is wrong because:
def get_list1(text): # text is a big text with \n in it result=[] # iterates the big blob of text as single characters for row in text: result.append(row) return result
If you can not use any split()
you can parse your file once character at time:
def get_list1(text):
"""This function takes a text with included newlines \n, and processes it
characterwise. Each character is added into a list of lists where the last
of it is always a list. A character is added to that last inner list
if it is not a \n.
If a \n is encountered, the last inner list is converted to a string
(if not empty - to account for newlines in the input) and a new empty
list is added for the characters of the next line. At the end the case
of last character == \n is handled. A list of line texts is returned."""
result=[[]]
for character in text:
if character == "\n":
t = ''.join(result[-1]).strip()
if len(t) > 0:
# change list of characters into string
result[-1] = int(t)
result.append([])
else:
result[-1].clear()
continue
result[-1].append(character)
try:
t = ''.join(result[-1]).strip()
if len(t) > 0:
# change list of characters into string
result[-1] = int(t)
else:
result = result[:-1]
except:
result = result[:-1]
return result
print(get_list1("1\n11\n111\n111\n11111\n11\n"))
Output:
[1, 11, 111, 111, 11111, 11]
Upvotes: 0
Reputation: 2277
You can use split()
:
def get_list1(text):
result=[]
for row in text:
result.append(row)
return result
with open("test.txt") as f:
n = f.read().split("\n")
l=get_list1(n)
print(l)
Or just use splitlines()
def get_list1(text):
result=[]
for row in text:
result.append(row)
return result
with open("test.txt") as f:
n = f.read().splitlines()
l=get_list1(n)
print(l)
Upvotes: 1