# The user is going to start the App by dragging a file icon # for a preprocessed csv file onto the Application/Program icon. # load_file() # There will be a call here to a load_file function but this # visualizer does not work with csv or GUIs like tkinter.The # call to load_file and the csv.reader or csv.DictReader loop # will be simulated by assigning a value for the number of rows # in the csv file to record_reference_gen(n). record_reference_gen(5) # there are 5 records or rows/lines #def load_csv_file(): # num_recordlines = 0 # try: #add error catching code # with open('/path/to/file/test.csv') as csv_file: # reader = csv.Dictreader(csv_file, delimiter=',') # for row in reader: # num_recordlines +=1 # firstname = row['firstname'] # lastname = row['lastname'] # address = row['address'] ### Problem 1 #The csv file loaded in has only three fields of data being #extracted for preview in a table with more than three #fields/columns. #Let's say the preview table has five(5) columns. #The csv file does not have field 3 (col_name = field3) and #field 4 (col_name = field4) or data for those #fields.These fields/columns have been created for the preview #table but their data/content needs to be created and added to #each row/line of data loaded in. # # Option 1 # Add the data for field3 and field4 within the csv.DictReader #loop or the csv.reader loop followed immediately after by the #table insertion code within the same loop. # #for row in reader: # num_recordlines +=1 # firstname = row['firstname'] # lastname = row['lastname'] # address = row['address'] # field3 = 'a comment': minor problem if 'static' # field4 = record_id #trouble: record_id not defined # tree.insert(", 0, values=(firstname, lastname, address, field3, field4)) # The content for field 3 and field 4 is assigned to the field #names.Let csv.DictReader add the data to each dictionary #created by csv.DictReader for each row/line read in by #csv.DictReader. # This works ok for field 3 if the data is static and can be #hardcoded. # There might be an entry get/set complication if field 3 must #be editable. For convenience, it will be assumed to be #'static'and hardcoded as the string, 'some comment # #Field 4 is the major problem because it needs to be created #and then added to the dictionary for each row. ### Problem 1 #The csv file loaded in has only three fields of data being #extracted for preview in a table with more than three #fields/columns. #Let's say the preview table has five(5) columns. #The csv file does not have field 3 (col_name = field3) and #field 4 (col_name = field4) or data for those #fields.These fields/columns have been created for the preview #table but their data/content needs to be created and added to #each row/line of data loaded in. # # Option 1 # Add the data for field3 and field4 within the csv.DictReader #loop or the csv.reader loop followed immediately after by the #table insertion code within the same loop. # #for row in reader: # num_recordlines +=1 # firstname = row['firstname'] # lastname = row['lastname'] # address = row['address'] # field3 = 'a comment': minor problem if 'static' # field4 = record_id #trouble: record_id not defined # tree.insert(", 0, values=(firstname, lastname, address, field3, field4)) # The content for field 3 and field 4 is assigned to the field #names.Let csv.DictReader add the data to each dictionary #created by csv.DictReader for each row/line read in by #csv.DictReader. # This works ok for field 3 if the data is static and can be #hardcoded. # There might be an entry get/set complication if field 3 must #be editable. For convenience, it will be assumed to be #'static'and hardcoded as the string, 'some comment # #Field 4 is the major problem because it needs to be created #and then added to the dictionary for each row. ### Problem 1 #The csv file loaded in has only three fields of data being #extracted for preview in a table with more than three #fields/columns. #Let's say the preview table has five(5) columns. #The csv file does not have field 3 (col_name = field3) and #field 4 (col_name = field4) or data for those #fields.These fields/columns have been created for the preview #table but their data/content needs to be created and added to #each row/line of data loaded in. # # Option 1 # Add the data for field3 and field4 within the csv.DictReader #loop or the csv.reader loop followed immediately after by the #table insertion code within the same loop. # #for row in reader: # num_recordlines +=1 # firstname = row['firstname'] # lastname = row['lastname'] # address = row['address'] # field3 = 'a comment': minor problem if 'static' # field4 = record_id #trouble: record_id not defined # tree.insert(", 0, values=(firstname, lastname, address, field3, field4)) # The content for field 3 and field 4 is assigned to the field #names. Let csv.DictReader add the data to each dictionary #created by csv.DictReader for each row/line read in by #csv.DictReader. # This works ok for field 3 if the data is static and can be #hardcoded. # There might be an entry get/set complication if field 3 must #be editable. For convenience, it will assumed to be 'static' #and hardcoded as the string, 'some comment'. #Field 4 is the major problem because it needs to be created #and then added to the dictionary for each row. # #I am not sure it is a good idea to interrupt the reader with #an inner loop, a call to a function or a call to a generator #function. # # Option 1a: Add inner loop to reader loop(procedural amateur) #for row in reader: # num_recordlines +=1 # firstname = row['firstname'] # lastname = row['lastname'] # address = row['address'] # ADD LOOP TO CREATE Record ID #for i in range(num_recordlines): # record_num = int(start_num + i) # record_str = str(record_num) # new_record = record_str.zfill(3) # record_id = 'XLT' + new_record # record_ids.append(record_id) # ref_dict[i] = record_id # print(record_id) # print("Every record has a reference label.") # yield record_id ## for record in record_ids: # print("Record ID: {}".format(record)) # print("ALL DONE!") # ref_dict[i] = record_id # for k,v in ref_dict.items(): # print(k,v) # field3 = 'a comment': minor problem if 'static' # field4 = record_id #trouble: record_id not defined # tree.insert(", 0, values=(firstname, lastname, address, field3, field4)) # # # # Option 2 # Load the csv data into a csv.DictReader() list that holds a #list of dictionaries and # # ### Here is the second problem: #When the loop for creating the record_ids is placed in the #csv.DictReader loop, the last record ID is being inserted on #the first line and the order is reversed for all records/lines. #The first record_id appears on the last line. # Is this due to not using an ordered dictionary? # Is it some kind of stack-based insertion with LIFO or FILO? ### Problem 1 #The csv file loaded in has only #three fields of data being extracted for preview in a table #with more than three fields/columns.Let's say five(5) columns. #The csv file does not have field3 and field4 or data for those #fields.These fields/columns have been created for the preview #table but their data/content needs to be created and added to #each dictionary created by csv.DictReader for each row/line #read in by csv.DictReader. # # # field3 = 'a comment' # # field4 = record_id #tree.insert(", 0, values=(firstname, lastname, address, field3, field4)) ### Here is the second problem: #When the loop for creating the record_ids is placed in the #csv.DictReader loop, the last record ID is being inserted on #the first line and the order is reversed for all records/lines. #The first record_id appears on the last line. # Is this due to not using an ordered dictionary? # Is it some kind of stack-based insertion with LIFO or FILO? def record_reference_gen(number): '''This function creates a record reference string that is ordered sequentially for each record. E.g. The record reference string for the first record is: 'XLT001'. The record reference string for the second record is: 'XLT002'. ''' num_recordlines = number firstnames = [] lastnames = [] addresses = [] record_ids = [] field3_data = [] field4_data = [] all_fields_aggregate =[] start_num = 101 record_ids = [] ref_dict = collections.OrderedDict() ref_dict = {} # number = number + 5 # print("Number + 5 = {}".format(number)) for i in range(num_recordlines): record_num = int(start_num + i) record_str = str(record_num) new_record = record_str.zfill(3) record_id = 'XLT' + new_record record_ids.append(record_id) # ref_dict[i] = record_id print(record_id) print("Every record has a reference label.") # yield record_id for record in record_ids: print("Record ID: {}".format(record)) print("ALL DONE!") # ref_dict[i] = record_id # for k,v in ref_dict.items(): # print(k,v) def math_calculations(): print("Welcome to the New World!") a = 7 if a < 8: print("a is less than 8") b = 6 z = type(a) eighter = 100 player = 50 print(z) print("type(a): {}".format(z)) print("a: {} b: {}".format(a,b)) return a,b if __name__ == "__main__": main()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question