Problem reading file manipualting values therein, Python

In summary, the code is not working because it is trying to do something that is not possible with the data that is being given to it.
  • #1
mr.me
49
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
  • #2
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:
  • #3
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
 
  • #4
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.
 
  • #5
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?
 
  • #6
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?
 
  • #7
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.
 

1. What is the purpose of manipulating values in a file using Python?

The purpose of manipulating values in a file using Python is to perform various operations on the data within the file, such as sorting, filtering, and transforming the data. This allows for more efficient and effective data analysis and processing.

2. How do you read a file in Python?

To read a file in Python, you can use the built-in open() function. This function takes in the name of the file and the mode in which you want to open it (e.g. read, write, append). Once the file is opened, you can use various methods to read and manipulate the data within it.

3. How do you manipulate values in a file using Python?

To manipulate values in a file using Python, you can use various built-in functions and methods. For example, you can use the read() method to read the entire file, the write() method to write data to the file, and the split() method to split the data into separate values. You can also use loops and conditional statements to perform more complex manipulations on the data.

4. Can you manipulate values in a specific column of a file using Python?

Yes, you can manipulate values in a specific column of a file using Python. One way to do this is by using the csv module, which allows you to read and write CSV (comma-separated values) files. You can use the csv.reader() function to iterate over the rows in the file, and then use indexing to access and manipulate specific columns within each row.

5. Is it possible to manipulate values in a file without actually reading the entire file into memory?

Yes, it is possible to manipulate values in a file without reading the entire file into memory. This can be done by using the with statement and the readline() method, which allows you to read and manipulate one line of the file at a time. This is useful for working with large files that may not fit into memory.

Similar threads

  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
21
Views
537
  • Programming and Computer Science
Replies
1
Views
541
  • Programming and Computer Science
Replies
16
Views
3K
Replies
6
Views
654
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
885
  • Programming and Computer Science
Replies
8
Views
879
  • Programming and Computer Science
Replies
1
Views
281
  • Programming and Computer Science
Replies
2
Views
649
Back
Top