简体   繁体   English

Python - 删除浮点数字符串

[英]Python - delete strings that are float numbers

I have a file with lots of lines that contain numbers seperated by coma , on each line.我有很多包含昏迷分隔号线的一个文件,每行。

Some lines contain numbers that are float numbers ie: ['9.3']有些行包含float numbers即: ['9.3']

i have been trying to delete those numbers from the list for about 1h~ but no luck.我一直试图从列表中删除这些数字大约 1 小时~但没有运气。 and whenever it tries to use int on a float number it gives error.并且每当它尝试在浮点数上使用 int 时,它都会出错。 Im not sure how to remove these floating numbers from the lines我不知道如何从行中删除这些浮点数

the numbers: https://pastebin.com/7vveTxjW数字: https : //pastebin.com/7vveTxjW

this is what i've done so far:这是我到目前为止所做的:

with open('planets.txt','r') as f:
    lst = []
    temp = []
    for line in f:
        l = line.strip()
        l = l.split(',')
        for x in l:
            if x == '':
                l[l.index(x)] = 0
            elif x == '\n' or x == '0':
                print "removed value", l[l.index(x)]
                del l[l.index(x)]

        try:
            temp.append([int(y) for y in l])
        except ValueError:
            pass

First off, modifying the list you are iterating over is a bad idea.首先,修改您正在迭代的列表是一个坏主意。 A better idea might be to construct a new list from the elements that can be converted to an int .一个更好的主意可能是从可以转换为int的元素构建一个新列表。

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

with open('planets.txt','r') as f:
    lst = []
    temp = []
    for line in f:
        l = line.strip().split(',')

        temp.append([int(y) for y in l if is_int(y)])

If you want to include the float values' integral component, you can do:如果要包含float值的积分组件,可以执行以下操作:

def is_float(element):
    try:
        float(element)
    except ValueError:
        return False
    return True

with open('planets.txt','r') as f:
    lst = []
    temp = []
    for line in f:
        l = line.strip().split(',')

        temp.append([int(float(y)) for y in l if is_float(y)])

looks like youre over complicating it, once you have got all your numbers from the file to a list just run this看起来你把它复杂化了,一旦你把文件中的所有数字都放到了一个列表中,就运行这个

numbers=["8","9","10.5","11.1"]
intList=[]
for num in numbers:
    try:
        int_test=int(num)
        intList.append(num)
    except ValueError:
        pass

just change my numbers list to your list name只需将我的号码列表更改为您的列表名称

当您获得包含数字的列表(例如, ['1','2.3','4.5','1.0'] )时,您可以使用以下内容

intDigits = [ i for i in digits if float(i) - int(float(i)) == 0]

Doing int() on a float shouldn't give an error.在浮点数上执行 int() 不应该给出错误。 Maybe your 'float' is actually a string as you're reading from the file, because也许您的“浮动”实际上是您从文件中读取的字符串,因为

int('9.3')

doesn't work but不起作用但是

int(9.3)

does.做。

Edit: How about applying this function to every number编辑:如何将此函数应用于每个数字

def intify(n):
    if n == '':
        return n
    try:
        return int(n)
    except ValueError:
        return int(float(n))

You can just match digits followed by a point followed by more digits:您可以只匹配数字后跟一个点后跟更多数字:

import re

output_list = []
input = open('input.txt', 'r')
for line in input:
    if '.' not in line:
        output_list.append(line)
    else:
        output_list.append(re.sub(r"(\d+\.\d+)", '', line))
        print("Removed", re.search(r"(\d+\.\d+)", line).group(1))

I would keep the numbers as string and simply check if there is a 'dot' in each 'splitted item' to identify floats.我会将数字保留为字符串,并简单地检查每个“拆分项目”中是否有一个“点”来识别浮点数。

with open('planets.txt','r') as f:
    for line in f:
        line = ','.join([item for item in line.strip().split(',') if not '.' in item])
        print(line)
        # ... write to file ...

If you just need to remove floats this was the simplest method for me.如果您只需要删除浮动,这对我来说是最简单的方法。

mixed = [1,float(1),2,3,4.3,5]

ints = [i for i in mixed if type(i) is not float]

Results in: [1, 2, 3, 5]结果:[1, 2, 3, 5]

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

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