![]() |
reading txt file putting in list function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: reading txt file putting in list function (/thread-19857.html) |
reading txt file putting in list function - Expel - Jul-17-2019 Having issues putting numbers from text file into a list and then calling that list to sum / avg numbers. trying to do it without importing, though i found a way to do it with "Import RE" with open('nums.txt') as f: fdata=f.read() print(fdata) myList = [] for A in fdata: myList.append(int(A)) print("Total =", sum(myList)) def Average(myList): return sum(myList) / len (myList) print("Average =", round(Average(myList), 2)) RE: reading txt file putting in list function - ichabod801 - Jul-17-2019 It's going to depend a lot on your file format. Note that fdata is a string, so for A in fdata: will loop over the individual characters of the string. You would normally use the split method of the string to make a list of the values separated by a delimiter. Then you could read complete numbers, not just the digits.
RE: reading txt file putting in list function - Expel - Jul-17-2019 (Jul-17-2019, 01:56 PM)ichabod801 Wrote: It's going to depend a lot on your file format. Note that fdata is a string, so so i should str.split(separator, maxsplit) for A in fdata: myList.split(()A))? RE: reading txt file putting in list function - ichabod801 - Jul-17-2019 Well, again, that depends on the format of your data file. Can you show us a few lines of that file? RE: reading txt file putting in list function - Expel - Jul-17-2019 (Jul-17-2019, 02:31 PM)ichabod801 Wrote: Well, again, that depends on the format of your data file. Can you show us a few lines of that file? Its just a random number test. so it goes straight down Quote:95 RE: reading txt file putting in list function - perfringo - Jul-17-2019 I observe that this is second thread on same topic. There is no information about file format available but my random guess is: with open(“nums.txt”, “r”) as f: numbers = [int(num) for num in f.read().split()] RE: reading txt file putting in list function - ichabod801 - Jul-17-2019 Okay, then you just need to split by lines. The code perfringo posted is one way to do that. Another way, is to just loop over the file: with open('nums.txt') as nums_file: numbers = [] for line in nums_file: numbers.append(int(line))Or you can do a list comprehension like perfringo did. RE: reading txt file putting in list function - Expel - Jul-17-2019 (Jul-17-2019, 02:41 PM)perfringo Wrote: I observe that this is second thread on same topic. Sry changed the question from the other, ill be moving back to that topic soon about the IOERRORS. (Jul-17-2019, 02:54 PM)ichabod801 Wrote: Okay, then you just need to split by lines. The code perfringo posted is one way to do that. Another way, is to just loop over the file:Works perfectly with open('nums.txt') as nums_file: numbers = [] for line in nums_file: numbers.append(int(line)) print("Total =", sum(numbers)) def Average(numbers): return sum(numbers) / len (numbers) print("Average =", round(Average(numbers), 2)) |