Python Forum
Reading integers from a file; the problem may be the newline characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading integers from a file; the problem may be the newline characters
#1
I want to read integers from a file. The delimiter is " " (a space).
I am getting the list of strings read into the program, but the error message is:

ValueError: invalid literal for int() with base 10:

Here is my code:

import os
os.path.join("Users", "admin", "PycharmProjects", "althhoff", "open_nums_test.txt")
f = open('open_nums_test.txt', "r")
test_list = f.readlines()
print (test_list)
test_list = list(map(int, test_list))
print("Integer list is : " + str(test_list))

When I print(test_list), I notice than both lists end in newlines - '\n'. Is the newline character causing the ValueError? Is map choking as it tries to int a "\n"? If so, what can I do about it? Wall
Reply


Messages In This Thread
Reading integers from a file; the problem may be the newline characters - by JRWoodwardMSW - Jul-14-2020, 12:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 402 Jan-22-2024, 12:56 PM
Last Post: deanhystad
Sad problems with reading csv file. MassiJames 3 608 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Facing issue in python regex newline match Shr 6 1,263 Oct-25-2023, 09:42 AM
Last Post: Shr
  Reading a file name fron a folder on my desktop Fiona 4 889 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,086 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Question How to append integers from file to list? Milan 8 1,442 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  Reading a file JonWayn 3 1,089 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 968 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 889 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Replace columns indexes reading a XSLX file Larry1888 2 975 Nov-18-2022, 10:16 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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