简体   繁体   中英

TypeError: unsupported operand type(s) for /: 'str' and 'int' for

I am using python 3.7

My list data looks like this

[['file1.o', '.text', '0x30'], ['file2.o', '.initvector', '0x36'], ['15', '31', '0x72']]

My code

For parsing the file

 c = re.compile("^\w+.*(\w+)\s+([\.\w]+)\s+([\w\.]+).*$")

Printing to a csv file uses

 for i in module_info:
    row = [i[0], i[1], "%d" %(i[2]/1024)]
    writer.writerow(row) 

I am getting this error:

TypeError: unsupported operand type(s) for /: 'str' and 'int'

How can I fix this?

I have updated the answer. Appreciate all contributions

The error is generating in i[2]/1024 this snippet of code, where the i[2] is actually interpreted as a string and 1024 as an int. That's why the error is there.

You have to convert the string into a number

To convert the hex string into a decimal number, use int(i[2], 16)

To convert the hex string into a decimal number, use bin(int(i[2], 16))

Use your preferred type of conversion and use it. I hope that'll solve your issue.

Since all the second-index elements are hexadecimal integers, you can use the int() function and pass 16 (base 16) as the second argument.

for i in module_info:
    row = [i[0], i[1], "%d" % (int(i[2], 16) / 1024)]
    writer.writerow(row) 

When looping through the list and printing row :

>>> for i in lst:
        row = [i[0], i[1], "%d" % (int(i[2], 16) / 1024)]
        print(row)

    
['file1.o', '.text', '0']
['file2.o', '.initvector', '0']
['15', '31', '0']

The second element of row in each iteration is "0" because the "%d" placeholder inserts (int(i[2], 16) / 1024) as an integer. I would recommend just inserting as a string (use "%s" ) if you want the full decimal representation.

>>> for i in lst:
        row = [i[0], i[1], "%s" % (int(i[2], 16) / 1024)]
        print(row)

    
['file1.o', '.text', '0.046875']
['file2.o', '.initvector', '0.052734375']
['15', '31', '0.111328125']

I changed the way I was parsing the file and reading its contents. I attempted to use greedy quantifier when I just needed to strip the "origin+".

#regex input format     Origin+Size        Section          Module
#                       800a1010+000c7c    .Code.Cpu0       Adc.o    #old line

 c = re.compile("^\w+.*(\w+)\s+([\.\w]+)\s+([\w\.]+).*$")

 #new line
 c = re.compile("^\w+\+(\w+)\s+([\.\w]+)\s+([\w\.]+).*$")

so now writing to file changes to this

for i in module_info:
    row = [i[0], i[1], (i[2])]
    writer.writerow(row)    

Also, the update that you posted does not explain how this code change provides the hex number in decimal, divide by 1024. Care to elaborate?

division with 1024 was just to get the value in KiloBytes, while changing from hex to decimal. I decide to drop that and do such operations in excel.

Reference discussion on quantifiers that i got benefit from

Does this answer your question? How to convert a hex string to hex number

Does this answer your question? How to convert a hex string to hex number

Yes it does. I see it in a new light now.

i[2] is a string unless it looks like number. Try:

row = [i[0], i[1], "%d" %(int(i[2])/1024)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM