我如何制作1&quot” 1"进入整数?

发布于 2025-02-12 18:05:26 字数 524 浏览 1 评论 0原文

列表

ValueError                                Traceback (most recent call last)
<ipython-input-96-93a440c16f3d> in <module>()
     38 
     39 for i in p:
---> 40   Gene.append(int(i.replace("  /gene=","")))

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

在这种情况下,p < /code>是[' /gene =“ 1”',' /gene =“ 2”',…] < /code>的 int()不起作用。它不能将'“ 1”'变成1。有没有办法将'“ 1”'转换为1?这是什么?

In this case, p is a list that is [' /gene="1"',' /gene="2"',…]

ValueError                                Traceback (most recent call last)
<ipython-input-96-93a440c16f3d> in <module>()
     38 
     39 for i in p:
---> 40   Gene.append(int(i.replace("  /gene=","")))

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

You see, a simple int() doesn't work. It can't turn '"1"' into 1. Is there a way to turn '"1"' into 1? What is it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白衬杉格子梦 2025-02-19 18:05:27

我将使用 strip() 将这些修剪成像:

p=['  /gene="1"','  /gene="2"']
Gene=[]
for i in p:
  Gene.append(int(i.replace("  /gene=","").strip("\" ")))

或偶数:

p=['  /gene="1"','  /gene="2"']
Gene=[]
for i in p:
  Gene.append(int(i.strip("/gen=\" ")))

或者,如果您不想处理平等标志的左侧,请使用分区

p=['  /gene="1"','  /gene="2"']
Gene=[]
for s in p:
    _, _, value = s.partition('=')
    Gene.append(value.strip("\" "))

I’d use strip() to trim those down like:

p=['  /gene="1"','  /gene="2"']
Gene=[]
for i in p:
  Gene.append(int(i.replace("  /gene=","").strip("\" ")))

or even:

p=['  /gene="1"','  /gene="2"']
Gene=[]
for i in p:
  Gene.append(int(i.strip("/gen=\" ")))

or if you don't want to deal with the left side of the equals sign use partition:

p=['  /gene="1"','  /gene="2"']
Gene=[]
for s in p:
    _, _, value = s.partition('=')
    Gene.append(value.strip("\" "))
阪姬 2025-02-19 18:05:27

您还可以替换报价:

Gene.append(int(i.replace("  /gene=","").replace('"','').replace("'",'')))

但是,我建议您查看 regex 用于处理解析这样的场景。

You can replace the quotes also:

Gene.append(int(i.replace("  /gene=","").replace('"','').replace("'",'')))

However, I would recommend looking at RegEx for handling parsing scenarios like this one.

阳光下慵懒的猫 2025-02-19 18:05:27

您还必须删除数字周围的报价。

Gene.append(int(i.replace('  /gene=', '').replace('"', ''))

You have to remove the quotes around the number as well.

Gene.append(int(i.replace('  /gene=', '').replace('"', ''))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文