I know this question is probably as old as the hills and related questions seem pretty common on Stackflow but I can’t find an answer I saw not very long ago that looked very similar. I don’t want the answer but just a hint.
I am using the csv module and csv.DictReader to load data from 3 columns of a csv file into a tkinter treeview:
ways to shorten the path and filename would be helpful
def load_file
#with open(’/storage/emulated/0/Pydroid/GUI/Python - Import CSV File To Tkinter Table/test.csv’) as f:
reader = csv.DictReader(f, delimiter=’,’)
for row in reader:
num_recordlines +=1
firstname = row[‘firstname’]
lastname = row[‘lastname’]
address = row[‘address’]
tree.insert("", 0, values=(firstname, lastname, address))
This works great!
But what I really want to do is add data for two more columns/fields, and the data for column ‘E’/field4 is a sequential string that would be generated by a generator function if I was more familiar with generators.Basically, it’s just a record number. (e.g. XLT001, XLT002, …) I can add a string to every row/line/record of field3 with:
field3 = ‘some comment’
but the data for field4 changes for every row. I’m not sure it’s a good idea to stick a function or another loop into the csv reader loop. So, if I take the treeview insertion out of the csv reader loop, I get a list of dictionaries for each row, but the insertion code wants a dictionary and I seem to need to slice and maybe enumerate the list and update each row dictionary, maybe with a second for loop. Something like:
for dictionary in enumerate(row_list):
for key in row_list [dictionary[3:5]]:
dictionary.update({key:record_dict[n]} where record_dict[n] is the string value (e.g. ‘XLT001’) from an ordered dictionary based on the number of records loaded in (num_recordlines) or row number for the dictionary being updated. I’m not sure how to use the enumerate number for n.
I don’t want a complete solution, just a hint at the syntax for the comprehension.
The resulting dictionary goes into a for loop that feeds:
tree.insert("", 0, values=(firstname, lastname, address, field3, field4))
When I put my sequential string maker loop in the csv reader loop I got the values in reverse order
The reason the record ids are in descending order is because tree.insert ("", 0, … inserts each row as first CHILD.So each record is added to the top of the list. To get the records in ascending order, use:
tree.insert("", ‘end’, …
I found this out by adding numbers to the left of the names in the CSV file and saw that they were inserted in descending order.
Similar question? I have used row [0], row.append, firstnames.append(firstname), names[i] etc. to collect data into a list for writer.writerows (results) but I don’t think I want a list here yet. Maybe treeview will accept a list in a different form of ‘insert’.I don’t know.
result = {}
for row in reader:
key = row[0] # the fieldname
result[key] = row [1:]
result = {row[0]:row [1:] for row in reader}