In a hangman game I am making, I have a text file containing this:
[
[
['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]
]
]
So using file io, I wrote this:
f = open('words.py')
lines = f.readlines()
for line in lines:
cat1.append(line)
print(cat1)
But all I get is:
['[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']
How can I make it a list divided into the 4 subgroups, which are also in 4 sublists, and lastly how I remove the 'n'
s?
2
Answers
If you change the single quotes to double quotes on lines 3, 4, and 5, then your file would be valid json, in which case you can easily parse the whole thing with the
json
module:data.json:
Your script:
Result:
If you’re absolutely committed to not changing a single thing in your file, including quote marks, then I suppose you could also use
ast.literal_eval
. But this is not the idiomatic solution.Incidentally,
['apple ios,a useless operating system',1]
is valid syntax, but if you want this element to have a length of three like its siblings, you probably meant to write['apple ios','a useless operating system',1]
The file contains a valid Python literal, so you can use the
ast..literal_eval()
function to parse it directly into alist
:You could also change the first line of the file to
cat1 = [
and then use astatement instead of doing all that.