So I want to read each character (separated by spaces) of each line of a text file and add them to a seperate index of a list for each line.
file = open("theTextFile.txt", 'r+')
pal = []
words = file.split(' ')
pal.append(words)
print(pal)
split_line(file)
The text file has a single character separated by a space for each line. The editor wouldn't allow for my punctuation from the text file, but below is an example of what it looks like.
rotor
racec
Maybe you want like this?
words = []
with open('theTextFile.txt', 'r') as fp:
words = sum([l.strip().split(' ') for l in fp], [])
print(words)
This is what I did to solve my issue:
lines = open('theTextFile.txt','r')
secondList = []
for line in lines:
line = line.split()
secondList.append(line)
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.