I am trying to read a csv file exported from Microsoft Excel, looking like this:
114.9;280.9;501.5;0;W10;Firewall south;A;1;2;;
119.0;280.9;501.5;0;W10;Southern escape route;B;2;3;asdasdf;
120.5;280.0;501.5;0;W10;Southern escape route;C;3;4;;
The delimiter is semicolon and some of the columns might be empty. For example column 10 may be populated with asdasdf as an example. My code looks like this:
reader = csv.reader(open(inFile), delimiter=";")
nodesTable = window.getNodesTable()
pathsTable = window.getPathsTable()
clearTable(nodesTable)
clearTable(pathsTable)
for x, y, z, safe, mod, descr_node, name, start, end, descr_path in reader:
nodesTable.SetStringItem(counter, nodesTable_ID_x, x)
nodesTable.SetStringItem(counter, nodesTable_ID_y, y)
nodesTable.SetStringItem(counter, nodesTable_ID_z, z)
nodesTable.SetStringItem(counter, nodesTable_ID_SafeArea, safe)
nodesTable.SetStringItem(counter, nodesTable_ID_Module, mod)
nodesTable.SetStringItem(counter, nodesTable_ID_Description, descr_node)
pathsTable.SetStringItem(counter, pathsTable_ID_Name, name)
pathsTable.SetStringItem(counter, pathsTable_ID_StartNode, start)
pathsTable.SetStringItem(counter, pathsTable_ID_EndNode, end)
pathsTable.SetStringItem(counter, pathsTable_ID_Description, descr_path)
I am getting the error ValueError: too many values to unpack. What am I doing wrong here?
There are 10 semicolons in each line of your CSV file, which means, that the line will be splitted into 11 elements. In your
forloop, there are only 10 variables used. Change the line into:and ignore the
_variable, which will get the empty character between the last;in the line and the end of line.Or change your whole loop into: