2017-06-01 46 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]是有效的語法,但如果你想這個元素有三個像它的兄弟姐妹的長度,你大概的意思是寫['apple ios','a useless operating system',1]

0

該文件包含一個有效的Python字面,這樣你就可以直接使用ast..literal_eval()函數解析成一個list

import ast 

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

print(cat1) 

你也可以在文件的第一行更改爲cat1 = [,然後用而不是做一切,一個

from words import cat1 

聲明。