[英]Convert str with single and double quotations into int
使用python將'"1"'
或"'1'"
(字符串)轉換為1
(整數)的最佳解決方案是什么?
int()方法將返回此值錯誤消息
ValueError: invalid literal for int() with base 10: "'1'"
如果您嘗試int('"1"')
或int("'1'")
我使用的解決方案如下:
tmp = '"1"'
tmp = tmp.replace('"', "")
tmp
# output: 1
這種“解決方案”的缺陷在於內引號(單/雙)很重要。
我會用
tmp = tmp.strip("'\"")
這將從tmp
的開頭/結尾中刪除'
和"
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.