-2

I have a text file numbers.txt that has numbers listed like so

1,2,3,4,5
6,7,8,9,10
11,12,13,14,15

but when i read the text using this:

with open('numbers.txt') as data:
    line = data.readlines()
    File = [line[0].strip()]
    print(File)

it prints ['1,2,3,4,5']

but i also want to use the add function like so:

File1 = [line[1].strip().__add__(File)
print(File1)

it prints ['1,2,3,4,5', '6,7,8,9,10']

but i need it to print [1,2,3,4,5,6,7,8,9,10] so i can use the count function later on

How could i fix this?

I tried using the np.array function but i can't __add__ arrays

i tried converting it to a list list(line[1].strip()] but when i did, it made more quotes and 10 turned into this: ('1', '0') but i want the numbers complete

ddejohn
  • 8,775
  • 3
  • 17
  • 30
  • You have some typos in your code. Please [edit] to fix them. Namely, missing indentation after `with`, missing close-bracket in the `File1` line, and wrong closing bracket in `list(line[1].strip()]`. – wjandrea Aug 10 '23 at 03:02
  • Hint: use the `str.split()` method, then look up how to turn a string into a number. – ddejohn Aug 10 '23 at 03:04
  • You need to convert them to integers, `nums = [int(num) for num in line[0].strip().split(",")]`. Also, it's very rare that you should call methods that start with `__`, see [How do I concatenate two lists in Python?](https://stackoverflow.com/q/1720421/12101554) – Samathingamajig Aug 10 '23 at 03:04
  • 2
    You **don't have** "a list of integers"; you have **a string**. (And there are no quotes to remove; that is only part of how the string is *represented*.) Please see the linked duplicate in order to understand how to process each line; you already know how to put the processed lists from each line together. (Do not call methods like `__add__` directly; they are there to *implement* `+`, so that you can use `+` with two lists.) – Karl Knechtel Aug 10 '23 at 03:05
  • BTW, welcome to Stack Overflow! Please take the [tour]. Check out [How to ask a good question](/help/how-to-ask) for tips, as well as [formatting help](/editing-help). – wjandrea Aug 10 '23 at 03:09

0 Answers0