2017-06-01 6 views
0

は、私が作ってい絞首刑執行人のゲームでは、私はこれを含むテキストファイルがあります。ファイルからリストを取得していますか?

[ 
[ 
['microsoft windows','common operating system',1], 
['apple ios,a useless operating system',1], 
['chromeos','a very simple OS developed by chrome',1], 
["linux","the most accesable and custimizable OS",1] 
], 
[ 
["photoshop", "the most popular image editting program",2], 
["adobe flash","basic animating and programming program",2], 
["pineapple","a spikey sweet fruit",2], 
["chrome", "a web browser",2] 
], 
[ 
["gpu","a computers _____ proccesing unit", 3], 
["tree", "a large plant found every where",3], 
["kangaroo","a marsupial found in the outback",3], 
["hawiian pizza","a very disputed type of triangular food",3] 
], 
[ 
["negev","a light machine gun, from csgo", 4], 
["revolver","a high caliber hand canon", 4], 
["desert eagle", "a infamous hand canon, highly praised in the USA", 4], 
["karambit knife","a blade shaped like a claw, know as very deadly", 4] 
] 
] 

をので、ファイルIOを使用して、私はこれを書いた:

f = open('words.py') 
lines = f.readlines() 
for line in lines: 
    cat1.append(line) 
print(cat1) 

をしかし、私が得るすべては次のとおりです。

['[\n', '[\n', "['microsoft windows','common operating system',1],\n", "['apple ios,a useless operating system',1],\n", "['chromeos','a very simple OS developed by chrome',1],\n", '["linux","the most accesable and custimizable OS",1]\n', '],\n', '[\n', '["photoshop", "the most popular image editting program",2],\n', '["adobe flash","basic animating and programming program",2],\n', '["pineapple","a spikey sweet fruit",2],\n', '["chrome", "a web browser",2]\n', '],\n', '[\n', '["gpu","a computers _____ proccesing unit", 3],\n', '["tree", "a large plant found every where",3],\n', '["kangaroo","a marsupial found in the outback",3],\n', '["hawiian pizza","a very disputed type of triangular food",3]\n', '],\n', '[\n', '["negev","a light machine gun, from csgo", 4],\n', '["revolver","a high caliber hand canon", 4],\n', '["desert eagle", "a infamous hand canon, highly praised in the USA", 4],\n', '["karambit knife","a blade shaped like a claw, know as very deadly", 4]\n', ']\n', ']\n'] 

リストを4つのサブリストに分かれた4つのサブグループに分割するにはどうしたらいいですか?最後に'\n'を削除する方法はありますか?

答えて

0

あなたが二重線3上の引用符、4、および5に単一引用符を変更する場合は、あなたのファイルはあなたが簡単にjsonモジュールで全体を解析することができ、その場合には有効なJSON、次のようになります。

データ.json:

 [ 
     [ 
     ["microsoft windows","common operating system",1], 
     ["apple ios,a useless operating system",1], 
     ["chromeos","a very simple OS developed by chrome",1], 
     ["linux","the most accesable and custimizable OS",1] 
     ], 
     [ 
     ["photoshop", "the most popular image editting program",2], 
     ["adobe flash","basic animating and programming program",2], 
     ["pineapple","a spikey sweet fruit",2], 
     ["chrome", "a web browser",2] 
     ], 
     [ 
     ["gpu","a computers _____ proccesing unit", 3], 
     ["tree", "a large plant found every where",3], 
     ["kangaroo","a marsupial found in the outback",3], 
     ["hawiian pizza","a very disputed type of triangular food",3] 
     ], 
     [ 
     ["negev","a light machine gun, from csgo", 4], 
     ["revolver","a high caliber hand canon", 4], 
     ["desert eagle", "a infamous hand canon, highly praised in the USA", 4], 
     ["karambit knife","a blade shaped like a claw, know as very deadly", 4] 
     ] 
     ] 

あなたのスクリプト:

import json 
with open("data.json") as file: 
    seq = json.load(file) 
print(seq) 

結果:

[[['microsoft windows', 'common operating system', 1], ['apple ios,a useless operating system', 1], ['chromeos', 'a very simple OS developed by chrome', 1], ['linux', 'the most accesable and custimizable OS', 1]], [['photoshop', 'the most popular image editting program', 2], ['adobe flash', 'basic animating and programming program', 2], ['pineapple', 'a spikey sweet fruit', 2], ['chrome', 'a web browser', 2]], [['gpu', 'a computers _____ proccesing unit', 3], ['tree', 'a large plant found every where', 3], ['kangaroo', 'a marsupial found in the outback', 3], ['hawiian pizza', 'a very disputed type of triangular food', 3]], [['negev', 'a light machine gun, from csgo', 4], ['revolver', 'a high caliber hand canon', 4], ['desert eagle', 'a infamous hand canon, highly praised in the USA', 4], ['karambit knife', 'a blade shaped like a claw, know as very deadly', 4]]] 

あなたが引用符を含むファイル内の単一のものを、変更しないと絶対にコミットしている場合は、私はあなたにもast.literal_evalを使用する可能性があるとします。しかし、これは慣用的な解決策ではありません。

import ast 
with open("data.dat") as file: 
    seq = ast.literal_eval(file.read()) 
print(seq) 

ちなみに、['apple ios,a useless operating system',1]は有効な構文ですが、あなたは、この要素は兄弟のように3の長さを持つようにしたい場合、あなたはおそらく['apple ios','a useless operating system',1]

0

ファイルがリテラル有効なPythonが含まれていますを書くことを意味し、あなたはlistに直接それを解析するast..literal_eval()機能を使用できるように:

import ast 

with open('words.py') as file: 
    cat1 = ast.literal_eval(file.read()) 

print(cat1) 

ますまたcat1 = [に、ファイルの最初の行を変更して、代わりにすべてのことを行うの

from words import cat1 

ステートメントを使用することができます。

関連する問題