【问题标题】:Replace floats with ints in text files用文本文件中的整数替换浮点数
【发布时间】:2020-06-18 02:27:23
【问题描述】:

我想在几个文本文件中查找浮点数并将其替换为整数。
我要转换的每个文本文件都有一个浮点值。它总是在特定关键字之后,并且必须乘以 10.000。
例如浮点数 1.5 应该变成整数 15.000
不过 1.5 之后的其他浮点数不必更改

def edit(file):
    with open(file, 'r') as f:
        filedata = f.read()
        for line in filedata:
           if "keyword" in line:
              filedata = filedata.replace(re.search(r"\d+\.\d+", line).group(), str(10000*re.search(r"\d+\.\d+", line).group()))
    with open(file, 'w') as f:
        f.write(filedata)

我试图用正则表达式替换浮点数。但这不起作用

示例文件摘录

abcdef 178 211 208 220    
ghijkl 0 0 0 0  
keyword 1.50 1.63 1.56 1.45

【问题讨论】:

  • 你能告诉我们输入文件的例子吗?
  • 是的,我会修改问题

标签: python text type-conversion integer


【解决方案1】:

您可以使用lines = filedata.split("\n") 遍历行。请小心,因为filedata 是一个包含整个文件的大字符串。当您执行for line in filedata 时,您遍历了文件的每个字符...

我还使用了另一种方式(没有regex)来查找数字并更改它们。

def edit(file):
    with open(file, "r") as f:
        filedata = f.read()
        lines = filedata.split("\n") # list of lines
        for index, line in enumerate(lines):
            if "keyword" in line:
                words = line.split() # ['keyword', '1.50', '1.63', '1.56', '1.45']
                for i, w in enumerate(words):
                    try:
                        # transform number to float, multiply by 10000
                        # then transform to integer, then back to string
                        new_word = str(int(float(w)*10000))
                        words[i] = new_word
                    except:
                        pass
                lines[index] = " ".join(words)
        new_data = "\n".join(lines) # store new data to overwrite file


    with open(file, "w") as f: # open file with write permission
        f.write(new_data) # overwrite the file with our modified data

edit("myfile.txt")

输出:

# myfile.txt
abcdef 178 211 208 220    
ghijkl 0 0 0 0  
keyword 15000 16299 15600 14500

编辑:更紧凑的方式

def edit(file):
    with open(file, "r") as f:
        filedata = f.read()
        line = [x for x in filedata.split("\n") if "keyword" in x][0]
        new_line = line
        for word in line.split():
            try: new_line = new_line.replace(word, str(int(float(word)*10000)))
            except: pass
    with open(file, "w") as f: # open file with write permission
        f.write(filedata.replace(line, new_line)) # overwrite the file with our modified data

edit("myfile.txt")

【讨论】:

  • @Wessowang :以更紧凑的方式查看我的编辑 :)
【解决方案2】:

当你发现自己在循环中使用正则表达式时,你应该在循环之外编译它。

接下来,如果你想替换一行中的一个值,你不应该在整个文件中搜索它。

最后,您必须将字符串转换为数字类型才能对其进行操作。如果你不这样做,你只会重复字符串('10' * 2'1010' 不是 20 也不是 '20'

以下是您的代码可能的改进:

def edit(file):
    with open(file, 'r') as f:
        rx = re.compile(r"\d+\.\d+")        # compile the regex only once
        filedata = f.readlines()            # get a list of the lines of the file
        for i, line in enumerate(filedata): # and enumerate them
            if "keyword" in line:
                val = re.search(r"\d+\.\d+", line).group()   # split the complex line
                newval = str(int(float(val) * 10000))
                filedata[i] = line.replace(val, newval)      # replace only the current line
                break                                        # no need to proceed further
    with open(file, 'w') as f:
        f.write(filedata)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2023-01-27
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多