【问题标题】:Why does IsNumeric() return False when given a number from a text line为什么在给定文本行中的数字时 IsNumeric() 返回 False
【发布时间】:2021-11-16 03:26:36
【问题描述】:

我正在编写一些 python 代码来评估文件的每一行,如果该行是一个数字,它应该返回 false 并忽略该行。此代码来自https://github.com/geoff604/sbv2txt/blob/master/README.md,我正在努力修改它。

但是,无论哪一行被传递给 IsNumeric() 函数,它的计算结果仍然为 False。我将相同的数字硬编码为字符串"2",并将其正确评估为True

在评估文本行时我是否遗漏了什么?

import sys

def isCaptionText(lineIndex):
    if lineIndex.isnumeric():
        print('True')
        return False
    else:
        return lineIndex

if len(sys.argv) < 3:
    print('Arguments: [source sbv filename] [destination txt filename]')
    sys.exit()

with open(sys.argv[1]) as f1:
    with open(sys.argv[2], 'a') as f2:
        lines = f1.readlines()
        for index, line in enumerate(lines):
            if isCaptionText(line):
                f2.write(line)

print('Output complete. File written as ' + sys.argv[2])

我正在分析的文件是我将在下面以缩写形式列出的文本。

2
00:00:04,360 --> 00:00:08,861
St. Louis' home for arts,
education and culture.

3
00:00:08,861 --> 00:00:11,444
(upbeat music)

4
00:00:12,290 --> 00:00:13,610
- [Woman] But we're in a global pandemic.

5
00:00:13,610 --> 00:00:16,000
We're also in a global blood shortage.

6
00:00:16,000 --> 00:00:18,230
- [Man] The more I dug,
the more it took me back

【问题讨论】:

    标签: python python-3.x isnumeric


    【解决方案1】:

    因此,每当您有新行时,python 都会看到文本和新行。例如:如果该行是1f1.readlines() 将其视为1\n,因此isNumeric 将返回false。这里的技巧是使用strip

    for index, line in enumerate(Lines):
    if isCaptionText(line.strip()):
       print(line)
    

    【讨论】:

    • @user3324136 如果你认为这是正确的答案,你能接受吗?
    【解决方案2】:
    • 因为文件中的每一行都有一个换行符,这取决于你使用的操作系统,比如第一行2,在Windows中实际上是2\n
    • 如果您想在特定操作系统上运行代码,可以使用replacestrip 来获取换行符。

    Windows 上的示例固定代码:

    with open("input.txt") as f1:
        lines = f1.readlines()
        for index, line in enumerate(lines):
            print(line.replace("\n","").isnumeric())
    
    input.txt
    
    2
    00:00:04,360 --> 00:00:08,861
    St. Louis' home for arts,
    education and culture.
    
    3
    00:00:08,861 --> 00:00:11,444
    (upbeat music)
    
    4
    00:00:12,290 --> 00:00:13,610
    - [Woman] But we're in a global pandemic.
    
    5
    00:00:13,610 --> 00:00:16,000
    We're also in a global blood shortage.
    
    6
    00:00:16,000 --> 00:00:18,230
    - [Man] The more I dug,
    the more it took me back
    

    结果:

    True
    False
    False
    False
    False
    True
    False
    False
    False
    True
    False
    False
    False
    True
    False
    False
    False
    True
    False
    False
    False
    

    【讨论】:

    • 感谢您的洞察力。这就说得通了。我很感激。
    【解决方案3】:

    使用 try 和 except 块

    try:
        int(lineIndex)
        print('True')
        return True
    except ValueError:
        return False
    

    或使用type()

    if type(lineIndex) == int:
        print('True')
        return False
    else:
        return lineIndex
    

    如果您有小数,请检查 float

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多