0

I built a UI on Linux and the exact same code works just fine on Linux but this portion of the code throws an error:

Traceback (most recent call last):
  File "C:\Users\sukhd\OneDrive\Desktop\projectBFC\BFCsource.py", line 169, in <module>
    main()
  File "C:\Users\sukhd\OneDrive\Desktop\projectBFC\BFCsource.py", line 164, in main
    form = MainWindow()
  File "C:\Users\sukhd\OneDrive\Desktop\projectBFC\BFCsource.py", line 21, in __init__
    self.convertCSVtoDB()
  File "C:\Users\sukhd\OneDrive\Desktop\projectBFC\BFCsource.py", line 95, in convertCSVtoDB
    (col1, col2, col3, col4, col5) = line.split(';')
ValueError: not enough values to unpack (expected 5, got 1)
>>> 

I can't seem to understand why this error comes up when I've tested this on Linux and it works just as intended.

def convertCSVtoDB(self):
        self.sortBroker()        
        conn = sqlite3.connect('brokers.db')
        cur = conn.cursor() 
        cur.execute('DELETE FROM Brokers')
        #cur.execute('''CREATE TABLE Brokers(CompanyName text,StreetAddress text,CityZIP text ,Telephone text,Email text)''')
        myData = []
        with open("sortedBrokers.csv") as f:
            # line = testName;testAddress;testZip,State;testphone;testemail
            for line in f.readlines():
                line = line.rstrip('\n')
                (col1, col2, col3, col4, col5) = line.split(';')
                mytuple = (col1, col2, col3, col4, col5)
                myData.append(mytuple)             
        with conn:
            cur.executemany('INSERT into Brokers (CompanyName,StreetAddress,CityZIP,Telephone,Email) VALUES (?,?,?,?,?)', myData)
            cur.execute("SELECT * FROM Brokers") 
            results = cur.fetchall()
        self.CB_brokers.clear()   
        for broker in range(len(results)):
            self.CB_brokers.addItem(str(results[broker][0]))
skular
  • 9
  • 5
  • 1
    Have you examined the value of `line` in both cases. If you're seeing different behavior, the value of `line` must be different. – Bryan Oakley Feb 15 '20 at 05:55
  • I was about to write what @BryanOakley did, the only way you can get that error is if the value of line is not what was excepted. As an aside, get rid of the `.readlines()` in the for loop, to end up with `for line in f:`. That last for loop should be a `for elem in results:`, not `for broker in range(len(results)):`. Also, you're not following PEP 8. – AMC Feb 15 '20 at 05:57
  • @AMC, thanks I made those changes. However, the issue still persists. I printed line before strip and after in both virtual box and windows and the output is the same. Except in Windows it crashes at (col1, col2... ) = line.split for some reason. – skular Feb 15 '20 at 06:33
  • @skular Relevant [csv-reader-different-outputs-on-ubuntu-and-windows-python](https://stackoverflow.com/questions/36494688), [`[python][csv] windows NewLine`](https://stackoverflow.com/search?q=isanswered%3Ayes+is%3Aquestion+%5Bpython%5D%5Bcsv%5D+windows+NewLine) – stovfl Feb 15 '20 at 09:18
  • ***"can you elaborate?"***: No, read it or leave it. – stovfl Feb 15 '20 at 20:21

0 Answers0