将单引号和双引号转换为int

sha*_*gtq 0 python types numpy python-3.x pandas

使用python 将'"1"'or或"'1'"(string)转换为1(int)的最佳解决方案是什么?

int()方法将返回此值错误消息

ValueError: invalid literal for int() with base 10: "'1'"
Run Code Online (Sandbox Code Playgroud)

如果你尝试int('"1"')int("'1'")

我使用的解决方案如下:

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

# output: 1
Run Code Online (Sandbox Code Playgroud)

这种"解决方案"的缺陷是内部引用(单/双)很重要.

Tim*_*ker 5

我会用

tmp = tmp.strip("'\"")
Run Code Online (Sandbox Code Playgroud)

这将删除'"从开始/结束 tmp.