The code for my program is:
linesheet = open("textfile.txt", mode ='r')
for entry in linesheet:
if '\n' in entry:
"textfile.txt" is a text file that consists of lines like:
So, to convert to integers, I remove the '\n', and use int()
Then, its says:
Traceback (most recent call last):
File "C:/Python33/python_file.py", line 10, in <module>
ValueError: invalid literal for int() with base 10: '470\n'
To try debugging, I printed the "entry" before the .replace()
It still has a newline!
The "if" block works, because that's where I added the print() to test.
- copied "linesheet" on to a list, in case the mode='r' didn't let me edit entry (which never happened before)
- printed all values everywhere (.replace never changed them!)
- copy pasted code into shell (it didn't work)
- tested all the functions I used (they worked just fine, including .replace)
- explicitly converted variables to str or int depending on what was needed.
The only problem I see is one line:
Which works in shell (without the linesheet/entry stuff)
but not in the program.
There is probably something wrong with the entry/linesheet then, but I copied linesheets items into a list, which I printed, and it looked fine. I then used that list in the program, and the first thing after that line of code is the replace block shown above, and it doesn't work.
Can someone help?