Python Forum

Full Version: reading txt file putting in list function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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))
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.
(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 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.

so i should
str.split(separator, maxsplit)
for A in fdata:
    myList.split(()A))
?
Well, again, that depends on the format of your data file. Can you show us a few lines of that file?
(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
90
89
53
88
100
101
35
72
93
87
63
100
98
75
65
80
92
25
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()]
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.
(Jul-17-2019, 02:41 PM)perfringo Wrote: [ -> ]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()]

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:

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.
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))