简体   繁体   中英

Convert str with single and double quotations into int

What is the best solution to convert a '"1"' or "'1'" (string) into 1 (int) using python?

int() method will return this value error message

ValueError: invalid literal for int() with base 10: "'1'"

if you try int('"1"') or int("'1'")

The solution that I used is as follow:

tmp = '"1"'
tmp = tmp.replace('"', "")
tmp

# output: 1

The flaw in this "solution" is that the inner quotation (single/double) matters.

I would use

tmp = tmp.strip("'\"")

This removes both ' and " from the start/end of tmp .

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