Just a warning I am as beginner as it gets with python so I might ask for clarification with answers if it's beyond what I've learned in class.
My text file is titled 'data.txt' and looks like this (the file ends at 80):
90
100
80
My code looks like this:
data = open('data.txt', 'r')
for line in data:
newLine = line.strip('\n')
dataList = newLine.split('\n')
#This closes the file
data.close()
print('The numbers are ', dataList)
countNumbers = len(dataList)
print('The count of the numbers is ', countNumbers)
When I run the program the output is:
The numbers are ['80']
The count of the numbers is 1
So I don't understand why I am not getting back all 3 elements of the list. Thank you.
CodePudding user response:
The reason why you are getting the last number is because you are just saving 1 item to the dataList variable in every loop, you are not creating a list. You are overwriting it at every loop.
I am not sure what your text file looks like but it seems like there are spaces between each line, so my data.txt file literally looks like this. It has 5 lines, 3 with something and 2 blank ones in between.
90
100
80
Okay so here is my code,
data = open('data.txt', 'r')
dataList = [] #create empty list
for line in data:
new = line.strip('\n')
if new: #check if there is data because when you strip a blank new line, you still get an empty string
dataList.append(new) #append line to dataList
data.close()
print('The numbers are ', dataList)
countNumbers = len(dataList)
print('The count of the numbers is ', countNumbers)
Here is my output,
The numbers are ['90', '100', '80']
The count of the numbers is 3
Here is an implementation with split that gives the same result. Not recommended because it's not as efficient since we know that there is only 1 item per line. We just need to get rid of the \n
(new line character).
data = open('data.txt', 'r')
dataList = []
for line in data:
new = line.split('\n')[0] #select first item in array
if new:
dataList.append(new)
print('The numbers are ', dataList)
countNumbers = len(dataList)
print('The count of the numbers is ', countNumbers)
CodePudding user response:
I did it this way:
data = open('data.txt', 'r')
dataList = []
for line in data:
if line != "\n":
dataList.append(line.replace("\n",""))
#This closes the file
data.close()
print('The numbers are ', dataList[:])
countNumbers = len(dataList[:])
print('The count of the numbers is ', countNumbers)
I hope it helps
CodePudding user response:
Use the read
method to turn the entire file into an array of strings, then iterate through the file:
data = open('data.txt', 'r')
dataList = []
for number in data.read().split("\n"):
if number != '':
dataList.append(int(number))
#This closes the file
data.close()
CodePudding user response:
You can do it just in one line, using the splitlines
method like this:
data = open('data.txt', 'r')
dataList = data.read().splitlines() # Put the file line by line in a List
dataList = list(filter(None, dataList)) # Remove all empty list elements
data.close()
print('The numbers are ', dataList)
countNumbers = len(dataList)
print('The count of the numbers is ', countNumbers)
CodePudding user response:
We could give a try to my approach buddy.I certainly hope this helps, pal!
with open('data.txt') as f:
lines_lst = [line.strip() for line in f.read().splitlines()]
[lines_lst.remove(el) for el in lines_lst if el ==""]
print('The numbers are ', lines_lst)
print('The count of the numbers is ', len(lines_lst))