Python Forum
Reading data into a Combobox from a .CSV file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading data into a Combobox from a .CSV file
#1
I believe that the method to achieve this will be straight forward but I cannot find how to read data from a .csv file into a combobox. How do I replace the hard coded values with the data contained in the file MAIN.csv


ComboGrp1 = ttk.Combobox(window, width = 10, height = 20)
ComboGrp1.place(x = 500 , y = 110)
# Hard code the values
ComboGrp1['values']=('Test 01', 'Test 02', 'Test 03')
import csv
with open('MAIN.csv', newline='') as csvfile:
      spamreader = csv.reader(csvfile, delimiter=',')
      for row in spamreader:
       print(', '.join(row))
Reply
#2
which GUI? one of: s://wiki.python.org/moin/GuiProgramming
Reply
#3
Apologies
This is not GUI related, I should have posted this in General coding, can it be moved there ?
Reply
#4
moved as per your request
Reply
#5
Thank you Larx60+ for relocating this where I should have put it.


I have delved into this further to see if I can get a solution..


Method 2: Prints out the first item from the .csv file and does not print any more lines.
The tuple misses the first line from the .csv file but gets picks up all the rest of the items.


Method 1: Using a tuple and a list to allow me to append each row to the list before converting back to a tuple.
Not very subtle but it works for the meantime.

I can't believe that there is not a better solution but my inexperience can't take me beyond this at the moment.


Another point that I note and need to understand is this: If I have any white space between words in the .csv for example "DADMIN Database Admin" when this is loaded into the combobox we get {DADMIN Database Admin} as the combobox selection. If we have other elements in the .csv such as "PIPING" or "ELEC" these appear as normal selections without the curly brackets, another riddle to solve.

I haven't thought to check on syntax within the brackets following the OPEN statement for example newline = '' etc.


# METHOD 1  use LIST and TUPLE TO obtain data from .csv file
with open('MAIN.csv', newline='') as csvfile:
      spamreader = csv.reader(csvfile , delimiter='"') 
      Tup1 = ()
      Lst1 = ()
      for row in spamreader:
       print(','.join(row))
       Lst1 = list(Tup1)
       Lst1.append(row)
       Tup1 = tuple(Lst1)


# METHOD #2       
# with open('MAIN.csv', newline='') as csvfile:
#      spamreader = csv.reader(csvfile , delimiter=',')
#      Tup1 = ()
#      for row in spamreader:
#       print(','.join(row))
#       Tup1 = [tuple(row)for row in spamreader]

      

ComboGrp1 = ttk.Combobox(window, width = 30, height = 20)
ComboGrp1.place(x = 500 , y = 110)
# Hard code the values
#ComboGrp1['values']=('Test1','Test2','Test3')
ComboGrp1['values']=(Tup1)

ComboGrp2 = ttk.Combobox(window, width = 10, height = 20)
ComboGrp2.place(x = 500 , y = 130)
I'd appreciate if anyone can shed light on these two questions.
Reply
#6
After a bit more searching a head scratching, I've tidied up METHOD #2 in the main adding the csvfile.closed line. This now just uses a tuple to load in the data from the .csv file into the combobox.

I have laid this out here such that it may be of some use to other newbies who may ask the same questions as me.

I now have 3 combo boxes and a hierarchical process, ComboGrp2 data is dependant on the selection made in ComboGrp1 and ComboGrp3 dependant on ComboGrp2.

Consider MAIN.csv as the seed file at the top of the hierarchy.

e.g loading .csv files for Vehicles > Cars > Fords as the user makes selections.


The callbacks for loading the appropriate .csv into the comboboxes are shown below.

The selections are saved in SQLite by .get and retrieved with .set statements.

In order to allow no selection each of the .csv files has a blank entry " " on row one.



# METHOD #2
with open('Menus_Vend/MAIN.csv', newline='') as csvfile:
      spamreader = csv.reader(csvfile , delimiter=',')
      Tup1 = ()
      for row in spamreader:
       Tup1 = [tuple(row)for row in spamreader]
       csvfile.closed

 
frame_Grps = Frame(window, relief = 'sunken', bd= 2, width = 180, height = 90 )
frame_Grps.place  (x = 700, y = 200)
     

ComboGrp1 = ttk.Combobox(frame_Grps, width = 10)
ComboGrp1.place(x = 10 ,y =10)
# Hard code the values for initial tests
#ComboGrp1['values']=('Test1','Test2','Test3')
ComboGrp1['values']=(Tup1)  #Data from the .csv file and tuple above METHOD #2


ComboGrp2 = ttk.Combobox(frame_Grps, width = 10)
ComboGrp2.place(x = 10 ,y =30)
#ComboGrp2['values']=(Tup2) # Assigned only after Grp1 Callback

ComboGrp3 = ttk.Combobox(frame_Grps, width = 10)
ComboGrp3.place(x = 10 ,y =50)
#ComboGrp3['values']=(Tup3) # Assigned only after Grp2 Callback

   ComboGrp1.bind("<<ComboboxSelected>>", CBackComboGrp1)
   ComboGrp2.bind("<<ComboboxSelected>>", CBackComboGrp2)


def CBackComboGrp1(eventObject):
 with open('Menus_Vend/'+ (eventObject.widget.get()) +'.csv', newline='') as csvfile:
      spamreader = csv.reader(csvfile , delimiter=',')
      Tup2 = ()
      for row in spamreader:
       Tup2 = [tuple(row)for row in spamreader]
       ComboGrp2['values']=(Tup2)
       csvfile.closed

       
def CBackComboGrp2(eventObject):
 with open('Menus_Vend/'+ (eventObject.widget.get()) +'.csv', newline='') as csvfile:
      spamreader = csv.reader(csvfile , delimiter=',')
      Tup3 = ()
      for row in spamreader:
       Tup3 = [tuple(row)for row in spamreader]
       ComboGrp3['values']=(Tup3)
       csvfile.closed     
    # FileSave
   Grp1 = ComboGrp1.get()    # new Grp1
   Grp2 = ComboGrp2.get()    #
   GRp3 = ComboGrp3.get()    # 

    # File Open
   ComboGrp1.set(id_exists[14])
   ComboGrp2.set(id_exists[15])
   ComboGrp3.set(id_exists[47])    # New Grp 3
Reply
#7
I want all comboboxes to have a blank line as the first item I have tried several ways to get spamreader to recognize a blank line (" ") with no success. I have reverted to using a list to allow me to insert a blank line example code below.


# METHOD #2
with open('Menus_Vend/MAIN.csv', newline='') as csvfile:
      spamreader = csv.reader(csvfile , delimiter=',')
      Tup1 = ()
      for row in spamreader:
       Tup1 = [tuple(row)for row in spamreader]
       List1 = list(Tup1)  #Create list from tuple
       List1.insert(0,'')  #Insert blank at index 0
       Tup1= tuple(List1)  #Convert new list back to tuple
       csvfile.closed
Reply
#8
Ref the above for the record I was using .csv files that were in the format.

"Car"
"Bus"
"Train"
"Boat"
"Motor cycle"


In the case of any entry with white space e.g "Motor cycle", this would appear in the combobox as {Motor cycle}

Having looked into this further and the .csv files I was using I can see that

a .csv file in the format ,Car,Bus,Train,Boat,Motor cycle the code below gives me what I was after.

In the case of the user selecting 'Car' as option 1 the callback loads the car options into combobox 2

,Ford,B M W,Seat,Honda,Volkswagen"

I believe that i am now providing .csv files in the correct way and using combobox and csv.reader in the correct way.


# METHOD #2
with open('Menus_Vend/MAIN.csv', newline='') as csvfile:
      spamreader = csv.reader(csvfile , delimiter=',')
      Tup1 = ()
      for Tup1 in spamreader:
       print(Tup1)
      csvfile.closed
ComboGrp1 = ttk.Combobox(frame_Grps, width=10)
ComboGrp1.place(x=40, y=10)
ComboGrp1['values']=(Tup1)
ComboGrp1.bind("<<ComboboxSelected>>", CBackComboGrp1)
def CBackComboGrp1(eventObject):

 with open('Menus_Vend/'+ (eventObject.widget.get()) +'.csv', newline='') as csvfile:
      spamreader = csv.reader(csvfile , delimiter=',')
      Tup2 = ()
      for Tup2 in spamreader:
        ComboGrp2['values'] = (Tup2)
        csvfile.closed
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Sad problems with reading csv file. MassiJames 3 559 Nov-16-2023, 03:41 PM
Last Post: snippsat
  Reading a file name fron a folder on my desktop Fiona 4 851 Aug-23-2023, 11:11 AM
Last Post: Axel_Erfurt
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Reading a file JonWayn 3 1,057 Dec-30-2022, 10:18 AM
Last Post: ibreeden
  Reading Specific Rows In a CSV File finndude 3 940 Dec-13-2022, 03:19 PM
Last Post: finndude
  Excel file reading problem max70990 1 865 Dec-11-2022, 07:00 PM
Last Post: deanhystad
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,273 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  Replace columns indexes reading a XSLX file Larry1888 2 951 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Reading Data from JSON tpolim008 2 1,031 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  Failing reading a file and cannot exit it... tester_V 8 1,753 Aug-19-2022, 10:27 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020