简体   繁体   中英

Reading text file contents into a list

I have a text file containing:

1:PAPER TOWNS,TOMORROWLAND
2:ENTOURAGE,JUPITER ASCENDING

and I'm planning to read them into a list which outputs:

[[1,'PAPERTOWNS','TOMORROWLAND'],[2,'ENTOURAGE','JUPITERASCENDING']]

I have written:

def read_file():
    fileName = "testing.txt"
    testFile = open(fileName)
    table = []

    for line in testFile:
        contents = line.strip().split(':')
        contents[0] = int(contents[0])
        contents[1] = contents[1].replace(' ','')
        table.append(contents)
    print(table)

I almost managed to get the output i wanted but i couldn't figure out a way to separate the strings from:

[[1,'PAPERTOWNS,TOMORROWLAND'],[2,'ENTOURAGE,JUPITERASCENDING']]

to

[[1,'PAPERTOWNS','TOMORROWLAND'],[2,'ENTOURAGE','JUPITERASCENDING']]

You can split the second element by comma.

Demo

def read_file():
    fileName = "testing.txt"
    testFile = open(fileName)
    table = []

    for line in testFile:
        contents = line.strip().split(':')
        table.append([int(contents[0])] + contents[1].split(","))
    print(table)

Output:

[[1, 'PAPER TOWNS', 'TOMORROWLAND'], [2, 'ENTOURAGE', 'JUPITER ASCENDING']]

Using Regex:

import re
def read_file():
    fileName = "testing.txt"
    testFile = open(fileName)
    table = []

    for line in testFile:
        contents = re.split("[,:]+", line.strip())
        table.append(contents)
    print(table)

Output:

[['1', 'PAPER TOWNS', 'TOMORROWLAND'], ['2', 'ENTOURAGE', 'JUPITER ASCENDING']]

This is a one-liner with pandas. Your file is like a CSV file, just the separator character can be colon or comma, so we use a regex:

import pandas as pd

df = pd.read_csv('file.txt', header=None, sep=r'[:,]')

You can split a string by multiple delimiters :

import re
print([[int(re.split(':|,', line.strip())[0])]+re.split(':|,', line.strip())[1:] for line in open('text_file','r')])

output:

[[1, 'PAPER TOWNS', 'TOMORROWLAND'], [2, 'ENTOURAGE', 'JUPITER ASCENDING']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM