Home > other >  Text to a list in python
Text to a list in python

Time:09-29

I have a bunch of text that I'd like to read in python and convert into a , separated list.

1800000000001
1000000000001
4200000000001
4000000000011
....
....
....
0400000000011
6420000000000
8660000000000
  1. Read text data

  2. Format text as strings and convert to a , separated

Final output

l = ['1800000000001', '1000000000001', '4200000000001', '4000000000011'.....]

Is there a way to do this without read the text data as csv?

CodePudding user response:

Just read the file

_file = open("file.txt","r")
out = _file.readlines()
print(out) #your data in a list

Maybe, you need to replace the char \n in the final strings.

out = [item.replace("\n","") for item in out]

CodePudding user response:

This method only needs to pass through your text once~

data = []
with open('file.txt') as f:
    for line in f:
        data.append(line.strip())
  • Related