Python Forum
How to append integers from file to list?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to append integers from file to list?
#3
I tried two solutions from the list, they being:
all_values = []

with open('message.txt', newline='') as infile:
    for line in infile:
        all_values.extend(int(line.strip().split(',')))
my_file = open("message.txt", "r")
  
# reading the file
data = my_file.read()
  
# replacing end of line('/n') with ' ' and
# splitting the text it further when '.' is seen.
data_into_list = int(data.replace('\n', ' ').split("."))
  
# printing the data
print(data_into_list)
my_file.close()
Both of them prompt the error
Error:
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
Reply


Messages In This Thread
RE: How to append integers from file to list? - by Milan - Mar-10-2023, 04:48 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  append str to list in dataclass flash77 6 639 Mar-14-2024, 06:26 PM
Last Post: flash77
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,312 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,599 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  Error "list indices must be integers or slices, not str" dee 2 1,532 Dec-30-2022, 05:38 PM
Last Post: dee
  read a text file, find all integers, append to list oldtrafford 12 3,805 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,269 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  Using .append() with list vs dataframe Mark17 7 11,043 Jun-12-2022, 06:54 PM
Last Post: Mark17
  TypeError: list indices must be integers or slices, not range Anldra12 2 2,707 Apr-22-2022, 10:56 AM
Last Post: Anldra12
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,385 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  list indices must be integers or slices, not lists error djwilson0495 2 2,930 Aug-27-2020, 06:13 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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