简体   繁体   English

如何在Python中将字符串中的数字引用转换为整数?

[英]How to convert a numerical reference in a string to an integer in Python?

I have a series of text files that include numerical references. 我有一系列包含数字参考的文本文件。 I have word tokenized them and I would like to be able to identify where tokens are numbers and convert them to integer format. 我已经用词对它们进行了标记,我希望能够识别标记在哪里,并将其转换为整数格式。

mysent = ['i','am','10','today']

I am unsure how to proceed given the immutability of strings. 考虑到字符串的不变性,我不确定如何进行。

请尝试[item if not item.isdigit() else int(item) for item in mysent]尝试[item if not item.isdigit() else int(item) for item in mysent]

If you try to convert a string that is not a representation of an int to an int , you get a ValueError . 如果尝试将不是int表示形式的string转换为int ,则会出现ValueError

You can try to convert all the elements to int , and catch ValueError s: 您可以尝试将所有元素转换为int ,并捕获ValueError

mysent = ['i','am','10','today']

for i in mysent:
    try:
        print(int(i))
    except ValueError:
        continue

OUTPUT: 输出:

10 10

If you want to directly modify the int inside mysent , you can use: 如果要直接修改mysent内部的int ,可以使用:

mysent = ['i','am','10','today']

for n, i in enumerate(mysent):
    try:
        mysent[n] = int(i)
    except ValueError:
        continue

print(mysent)

OUTPUT: 输出:

['i', 'am', 10, 'today'] ['i','am',10,'today']

.isdigit() IS NOT THE SAME AS try/except!!!! .isdigit()不同于try / except !!!!

In the comments has been pointed out that .isdigit() may be more elegant and obvious. 在评论中已指出.isdigit()可能更优雅,更明显。 As stated in the Zen of Python , There should be one-- and preferably only one --obvious way to do it . 正如Python Zen所述应该有一种-最好只有一种-显而易见的方法

From the official documentation , .isdigit() Return true if all characters in the string are digits and there is at least one character, false otherwise. 根据官方文档.isdigit() 如果字符串中的所有字符都是数字并且至少包含一个字符,则返回true.isdigit() 返回false。

Meanwhile, the try/except block catches the ValueError raised by applying int to a non-numerical string . 同时, try/except块捕获通过将int应用于非数字string引发的ValueError

They may look similar, but their behavior is really different: 它们可能看起来相似,但是它们的行为确实不同:

def is_int(n):
    try:
        int(n)
        return True
    except ValueError:
        return False

EXAMPLES: 例子:

Positive integer: 正整数:

n = "42"

print(is_int(n))   --> True
print(n.isdigit()) --> True

Positive float: 正浮点数:

n = "3.14"

print(is_int(n))   --> False
print(n.isdigit()) --> False

Negative integer: 负整数:

n = "-10"

print(is_int(n))   --> True
print(n.isdigit()) --> False

u hex: u十六进制:

n = "\u00B23455"

print(is_int(n))   --> False
print(n.isdigit()) --> True

These are only some example, and probably you can already tell which one suits better your needs. 这些只是一些示例,您可能已经知道哪个更适合您的需求。
The discussion open around which one should be used is exhausting and neverending, you can have a look a this couple of interesting SO QA: 围绕应该使用哪个的讨论展开了无穷无尽的工作,您可以看一下这两个有趣的SO QA:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM