csv - Python: Parse string objects into python objects from a file -
i've got .csv
file , need information it. if open file, can see 2 lines in it, says "data" , "notes", , need information these 2 variables have.
when open .csv
file, shows these lines:
data = [0,1,2,3,4,5,3,2,3,4,5,] notes = [{"text": "hello", "position":(2,3)}, {"text": "bye", "position":(4,5)}]
to open file use:
import csv class a() def __init__(self): #some stuff in here def get_data(self): file = open(self.file_name, "r") data = csv.reader(file, delimiter = "\t) rows = [row row in data]
now, read information in data, write:
line in row[1][0]: try: value_list = int(line) print value_list except valueerror: pass
and, can create list these values , print it. now, need read data "notes", can see, list dictionaries elements. need do, read "position" element inside each dictionary , print it.
this code have:
notes in row[3][0]: if notes["position"]: print notes["position"]
but this, gives me error: typeerror: string indices must integers, not str
how can access these elements of each dictionary , print it? hope can me.
this .csv
file trying information.
you can change last part of code to:
for note in eval(rows[3][0].strip("notes = ")): if note["position"]: print note["position"]
if need position actual tuple instead of string, can change last line to:
print tuple(note["position"])
Comments
Post a Comment