Python Forum
reading txt file putting in list function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
reading txt file putting in list function
#1
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))
Reply
#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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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))
?
Reply
#4
Well, again, that depends on the format of your data file. Can you show us a few lines of that file?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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
Reply
#6
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()]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
(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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad problems with reading csv file. MassiJames 3 619 Nov-16-2023, 03:41 PM
Last Post: snippsat
  trouble reading string/module from excel as a list popular_dog 0 416 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Reading a file name fron a folder on my desktop Fiona 4 898 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,089 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  function return boolean based on GPIO pin reading caslor 2 1,170 Feb-04-2023, 12:30 PM
Last Post: caslor
  Reading a file JonWayn 3 1,092 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 973 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 891 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 977 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Failing reading a file and cannot exit it... tester_V 8 1,800 Aug-19-2022, 10:27 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020