Converting unicode to 'int'

Hi,

I’m trying to convert numbers in a column from a CSV file to integers. It seems those numbers are of unicode type. The problem is that I get an error when calling int() on that number:

invalid literal for int() with base 10: ‘ink’

def setup():
    size(1000, 600, P3D)

    csv = loadTable("citrohanCSV.csv") 
    
    #0 - 1 - 2 - 3 - 4
    #id - x - y - z - links
    
    for ir in xrange(csv.getRowCount()):
        for ic in xrange(csv.getRow(ir).getColumnCount()):
            links = splitTokens(csv.getRow(ir).getString(4)[1:-1], ',')

            print links # returns a number like 127, 3918 or 41
            print type(links) # returns 'unicode'
            print int(links) # returns the error

I googled the error but couldn’t find a similar issue.

Questions:

  • How can I convert that unicode value to an integer ?
  • If that’s not possible what workaround would you suggest ?

(link to the csv file if needed)

1 Like

PApplet::splitTokens() returns a String[] array:
Processing.org/reference/splitTokens_.html

However, I don’t think Python’s native int() can convert whole arrays:
Py.Processing.org/reference/intconvert.html

But Processing’s int() can:
Processing.org/reference/intconvert_.html

However, that’s not its actual internal function’s name. It is parseInt() instead:
Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#parseInt-java.lang.String:A-

And as expected, PApplet::parseInt() isn’t available globally.
We need to use the this variable in order to access it: this.parseInt()

Anyways, that’s what I’ve come up with: :smiley:

# https://Discourse.Processing.org/t/converting-unicode-to-int/11047/2
# GoToLoop (v1.1.1) (2019/May/08)

FILENAME = 'citrohancsv.csv'
DELIMS = '[, ]'
ID, LINKS = 0, 4

parseInt = this.parseInt

csv = loadTable(FILENAME, 'header')

for tr in csv.rows():
    links = parseInt(splitTokens(tr.getString(LINKS), DELIMS))
    print tr.getInt(ID), links

print csv.columnTitles # ,x,y,z,links

exit()

citrohancsv.csv

,x,y,z,links
0,0.0,-25.0,111.0,"[1, 4, 5, 10, 273, 305]"
1,0.0,-25.0,143.0,"[0, 4, 11, 273, 274]"
2,0.0,-2.0,111.0,"[3, 6, 12, 13, 285, 286]"
3,0.0,-2.0,143.0,"[2, 7, 12, 286, 294]"
4,0.0,-27.0,145.0,"[0, 1, 5, 7, 8, 11, 12, 287, 288]"
5,0.0,-27.0,102.0,"[0, 4, 6, 8, 9, 10, 13, 287, 288, 304, 334]"
6,0.0,-12.0,111.0,"[2, 5, 7, 10, 11, 13, 285, 293]"
7,0.0,-12.0,143.0,"[3, 4, 6, 11, 12, 293, 294]"
8,0.0,-47.0,145.0,"[4, 5, 9, 287, 303]"
9,0.0,-47.0,102.0,"[5, 8, 303, 304]"
3 Likes

@GoToLoop Thank you so much for the swift reply and detailed explanations ! It works great now.