Home > Software design >  Invalid literal manipulation in a string [closed]
Invalid literal manipulation in a string [closed]

Time:09-22

When I am doing following, things work well:

Folder1/Folder2/YID 0413/Q/test.pdf
doc_id = name.split("/")[-3]
output - >YID 0413

error is in the following :

Folder1/Folder2/YID 0042.1/Q/test1.pdf
doc_id = name.split("/")[-3]
output - >Error - [ValueError] ("invalid literal for int() with base 10: '413.1'",)
output should be - YID 0042.1

datatype for YID 0413 is 'str'. 

The only way to get the desired value is by using split. How can I make sure both cases work fine?

CodePudding user response:

Not sure what you're code is doing as you only post pseudocode, but this works for me just fine:

t = "Folder1/Folder2/YID 0042.1/Q/test1.pdf"

doc_id = t.split("/")[-3]

print(doc_id)

# YID 0042.1


CodePudding user response:

The following code:

name = 'Folder1/Folder2/YID 0042.1/Q/test1.pdf'
doc_id = name.split("/")[-3]
print(doc_id)

Prints the following to the console:

YID 0042.1

You did not send the exact code you're having the issue with, so its unclear where your issue is.

The ValueError you gave occurs when you try to construct a int from a string with a float value, so I'm assuming there's a fair bit of code you aren't sharing.

  • Related