I'd like to take this list and write it to a new txt file. Ignoring the ':' , {}, and words within quotation marks. so the txt file created would look like this:
shapes = [{'type': 'circle', 'x': 300, 'y': 300, 'radius': 100, 'color': 'cyan'},
{'type': 'circle', 'x': 300, 'y': 300, 'radius': 10, 'color': 'white'},
{'type': 'rectangle', 'x1': 500, 'y1': 500, 'x2': 550, 'y2': 580, 'color': 'green'},
{'type': 'line', 'x': 0, 'y': 0, 'a': 100, 'b': 300, 'color': "black", 'width': 7},
{'type': 'point', 'x': 200, 'y': 50, 'color': 'black'},
{'type': 'point', 'x': 205, 'y': 50, 'color': 'black'},
{'type': 'point', 'x': 210, 'y': 50, 'color': 'black'},
{'type': 'triangle', 'x': 500, 'y': 100, 'a': 600, 'b': 100, 'c': 550, 'd': 200, 'color': 'yellow'},
{'type': 'oval', 'x': 100, 'y': 100, 'a': 400, 'b': 400, 'color': 'red'},
{'type': 'text', 'x': 500, 'y': 50, 'message': 'hello world!', 'color': 'blue'}
]
I'd like to take this list and write it to a new txt file. Ignoring the ':' , {}, and words within quotation marks. so the txt file created would look like this:
circle, 300,300, 100, cyan
circle, 300,300, 10, white
rectangle, 500,500, 550,580, green
line, 0,0, 100,300, black, 7
point, 200,50,black
point, 205,50,black
point, 210,50,black
triangle, 500,100, 600,100, 550,200, yellow
image, 150,500, cedar_rapids.ppm
oval, 100,100, 400,200, red
text, 500,50, hello world!, blue
what i have so far is:
s_shapes = ['{}\n'.format(i) for i in shapes]
textFileName = input("Save as: ")
with open(textFileName, "w") as fp:
fp.writelines(s_shapes)
fp.close()
it prints my list in the txt file, but doesnt get rid of the words in quotations, the : or the {}.
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images