Reputation: 3768
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']]
Upvotes: 0
Views: 63
Reputation: 12669
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']]
Upvotes: 0
Reputation: 82765
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']]
Upvotes: 1
Reputation: 33950
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'[:,]')
Upvotes: 0