简体   繁体   English

仅读取txt文件python中的数字

[英]Read only the numbers from a txt file python

I have a text file that contains these some words and a number written with a point in it. 我有一个文本文件,其中包含一些单词和一个带有点的数字。 For example 例如
hello! 54.123

Now I only want the number 54.123 to be extracted an converted so that the outcome is 54123 现在我只希望将数字54.123提取为转换后的结果,以便结果为54123

The code I tried is 我试过的代码是

import re
exp = re.compile(r'^[\+]?[0-9]')

my_list = []
with open('file.txt') as f:
    lines = f.readlines()
    for line in lines:
        if re.match(exp, line.strip()):
            my_list.append(int(line.strip()))

#convert to a string
listToStr = ' '.join([str(elem) for elem in my_list])
print(listToStr)

But this returns the error: ValueError: invalid literal for int() with base 10: '54.123' 但这会返回错误:ValueError:int()的无效文字,基数为10:“ 54.123”

Does anyone know a solution for this? 有谁知道解决方案吗?

This may help I am now getting numbers from the file I guess you were trying to use split in place of strip 这可能会帮助我现在从文件中获取数字,我猜您正在尝试使用split代替strip

import re
exp = re.compile(r'[0-9]')

my_list = []
with open('file.txt') as f:
    lines = f.readlines()
    for line in lines:
        for numbers in line.split():
            if re.match(exp, numbers):
                my_list.append(numbers)

#convert to a string
listToStr = ' '.join([str(elem) for elem in my_list])
print(listToStr)

You can check if a given line is a string representing a number using the isdigit() function. 您可以使用isdigit()函数检查给定的行是否为代表数字的字符串。

From what I can tell you need to just check if there is a number as isdigit() works on integers only (floats contain "." which isn't a number and it returns False). 据我所知,您只需要检查是否存在一个数字,因为isdigit()仅适用于整数(浮点数包含“。”,它不是数字,并且返回False)。

For example: 例如:

def numCheck(string):
  # Checks if the input string contains numbers
  return any(i.isdigit() for i in string)

string = '54.123'
print(numCheck(string)) # True

string = 'hello'
print(numCheck(string)) # False

Note: if your data contains things like 123ab56 then this won't be good for you. 注意:如果您的数据包含123ab56类的123ab56那么这对您就没有好处。


To convert 54.123 to 54123 you could use the replace(old, new) function. 要将54.123转换为54123,可以使用replace(old, new)函数。

For example: 例如:

string = 54.123
new_string = string.replace('.', '') # replace . with nothing
print(new_string) # 54123

You can try to convert the current line to a float. 您可以尝试将当前行转换为浮点数。 In case the line does not contain a legit float number it returns a ValueError exception that you can catch and just pass. 如果该行不包含合法的浮点数,则它将返回一个ValueError异常,您可以捕获该异常并将其传递。 If no exception is thrown just split the line at the dot, join the 2 parts, convert to int and add to the array. 如果没有异常,则将点处的线分开,将两部分合并,转换为int并添加到数组中。

my_list = []
with open('file.txt') as f:
    lines = f.readlines()
    for line in lines:
        try:
            tmp = float(line)
            num = int(''.join(line.split(".")))
            my_list.append(num)
        except ValueError:
            pass

#convert to a string
listToStr = ' '.join([str(elem) for elem in my_list])
print(listToStr)

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

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