2017-03-29 10 views
0

だから、テキストファイルの各行の各文字を(空白で区切って)読み込んで、それぞれのリストの別々のインデックスに追加したいライン。テキストファイルの各行の各文字をPythonのリストに追加する

file = open("theTextFile.txt", 'r+') 
pal = [] 

words = file.split(' ') 
pal.append(words) 

print(pal) 
split_line(file) 

テキストファイルには、行ごとにスペースで区切られた1文字があります。 エディタはテキストファイルの句読点を許可しませんが、以下はその例です。

R O T O R

たぶん、あなたはこのようにしたいのC E Cを

+0

[この](http://stackoverflow.com/questions/3277503/how-do-i-read-a-file行ごとに1行ずつ?rq = 1)が役立ちます。 –

+0

入力と期待される出力の短い例を挙げることができますか? – AndreyF

+0

Pythonでファイルを読み込んで[ここ](http://stackoverflow.com/questions/28781476/turning-list-from-text-file-news)のようなリストに出力する方法については、 in-python-list)、[ここ](http://stackoverflow.com/questions/35273534/python-read-lines-of-an-entire-file-and-efficiently-storing-theones-i-want -in)または[here](http://stackoverflow.com/questions/37002578/python-read-file-into-list-edit)これを理解することができます。しかし、あなたの質問は明らかではありません、あなたは "theTextFile.txt"の例を挙げ、あなたの期待される出力が何であるか教えてください。 – dirkgroten

答えて

0

これは私が私の問題を解決するためにやったことです:

lines = open('theTextFile.txt','r') 

secondList = [] 
for line in lines: 
    line = line.split() 
    secondList.append(line) 
0

rを?

words = [] 
with open('theTextFile.txt', 'r') as fp: 
    words = sum([l.strip().split(' ') for l in fp], []) 
print(words) 
関連する問題