Problem reading file manipualting values therein, Python

  • Context: Python 
  • Thread starter Thread starter mr.me
  • Start date Start date
  • Tags Tags
    File Python Reading
Click For Summary

Discussion Overview

The discussion revolves around a Python coding problem related to reading integers from a file, storing them in a list, and calculating their average. Participants are exploring the issues in the code provided and seeking clarification on how to properly handle string-to-integer conversions while reading from a file.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their attempt to read integers from a file and expresses confusion about why their code fails to work as intended.
  • Another participant points out multiple errors in the code and highlights a specific ValueError encountered when trying to convert a string representation of a list to an integer.
  • A participant explains that the 'int' function cannot convert the string representation of a list and suggests converting strings to numbers as they are read in.
  • Some participants discuss the presence of newline characters in the strings and how that affects conversion to integers.
  • There is a suggestion to convert the line read from the file directly to an integer before appending it to the list.
  • One participant reflects on their misunderstanding of how to manipulate the read data and expresses a desire to clarify their approach.

Areas of Agreement / Disagreement

Participants generally agree that the current approach to reading and converting data is flawed, but there is no consensus on the best method to correct it. Multiple viewpoints on how to handle string conversion and list manipulation are present.

Contextual Notes

Participants have not fully resolved the specific steps needed to convert strings to integers while reading from the file, and there are unresolved questions about handling newline characters and list structures.

mr.me
Messages
49
Reaction score
0
I was trying to write code that would read integers stored in a file, line by line and then save them to a list and average that list and display it, it didn't work. What can I do to correct this code, I believe that the error is in how I am treating the list but I am confused at why the interpreter can't understand it.

I'm still to this and appreciate the help

-- The attempt at a solution


Code:
if (os.path.isfile("Testing_1.txt")):
     inFile = open("Testing_1.txt", "r")
     list_to_av=[ ] 

     inLine = inFile.readline()
     while len(inLine) > 0:
        
          inLine = inFile.readline()
          list_to_av.append((inLine))
     print  int(str(list_to_av))/ len(list_to_av)
     inFile.close()
else:
     print "Testing_1.txt cannot be found."
 
Last edited:
Technology news on Phys.org
There are several errors in there. But it's best that you find them yourself. When I run that with a sample input file, I get:
ValueError: invalid literal for int() with base 10: "['2\\n', '3\\n', '']"
Doesn't that mean something to you? What is 'int' supposed to do?
 
Last edited:
int is supposed to convert the data type in this case the literal values aren't only "numbers" so it crashes

I understand that error but I don't understand what the correct approach is
 
int converts a string to a single number. If it can. And it doesn't sum over numbers in the string. "['2\\n', '3\\n', '']" doesn't convert to single number. It's a string representation of a list of strings. Maybe you should convert the strings to numbers as they are read in and sum the numbers? The error message from 'int' is giving you the string it was trying to convert. Try and empathize with 'int' and figure you why you would have a hard time converting that to a number.
 
I understand that int can't convert the strings in the list because of the \\n, because it is not a number like I said above

What I don't understand is where or how I use the list to provide me with something that is not a string but is and integer

If I attempt to take the strings and store them as they are read how would I convert them to ints?
 
mr.me said:
I understand that int can't convert the strings in the list because of the \\n, because it is not a number like I said above

What I don't understand is where or how I use the list to provide me with something that is not a string but is and integer

If I attempt to take the strings and store them as they are read how would I convert them to ints?

Why don't you convert inLine to an integer and store the integers into a list?
 
I see fo the reason that it has the read in it I didn't think I could convert inLine, and so never tried when fiddling around with the code, I wonder why I thought that.
 

Similar threads

Replies
3
Views
3K
Replies
1
Views
2K
Replies
35
Views
8K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K