Trying multiplying numbers on a line starting with the word “size” with a constant variable across 181 text files

情到浓时终转凉″ 提交于 2021-02-11 12:26:46

问题


I have a folder of 181 text file, each containing numbers but I only need to multiply those on lines that have "size" by a constant variable such as 0.5, but I did achieve this with this: Search and replace math operations with the result in Notepad++ But what I am trying to do is without expression or quotation marks so that the rest of the community I am in can simply do the same without editing every file to meet the format needed to multiply each number. For Example:

    farmers = {
        culture = armenian
        religion = coptic
        size = 11850
    }

being multiplied by 0.5 to:

    farmers = {
        culture = armenian
        religion = coptic
        size = 5925
    }

I tried making a python script but it did not work although I don't know much python:

import operator

with open('*.txt', 'r') as file:
    data = file.readlines()

factor = 0.5
count = 0

for index, line in enumerate(data):
    try:
        first_word = line.split()[0]
    except IndexError:
        pass

    if first_word == 'size':
        split_line = line.split(' ')
        # print(' '.join(split_line))
        # print(split_line)
        new_line = split_line

        new_line[-1] = ("{0:.6f}".format(float(split_line[-1]) * factor))
        new_line = ' '.join(new_line) + '\n'
        # print(new_line)

        data[index] = new_line

        count += 1

    elif first_word == 'text_scale':
        split_line = line.split(' ')
        # print(split_line)
        # print(' '.join(split_line))
        new_line = split_line

        new_line[-1] = "{0:.2f}".format(float(split_line[-1]) * factor)
        new_line = ' '.join(new_line) + '\n'
        # print(new_line)

        data[index] = new_line

        count += 1


with open('*.txt', 'w') as file:
    file.writelines(data)

print("Lines changed:", count)

So are there any solutions to this, I rather not make people in my community format every single file to work with my solution. Anything could work just that I haven't found a simple solution that is quick and easy for anyone to understand for those who use notepad++ or Sublime Text 3.


回答1:


If you use EmEditor, you can use the Replace in Files feature of EmEditor. In EmEditor, select Replace in Files on the Search menu (or press Ctrl + Shift + H), and enter:

Find: (?<=\s)size(.*?)(\d+)

Replace with: \J "size\1" + \2 / 2

File Types: *.txt (or file extension you are looking for)

In Folder: (folder path you are searching in)

Set the Keep Modified Files Open and Regular Expressions options (and Match Case option if size always appears in lower case),

Click the Replace All button.

Alternatively, if you would like to use a macro, this is a macro for you (you need to edit the folder path):

editor.ReplaceInFiles("(?<=\\s)size(.*?)(\\d+)","\\J \x22size\\1\x22 + \\2 / 2","E:\\Test\\*.txt",eeFindReplaceCase | eeFindReplaceRegExp | eeReplaceKeepOpen,0,"","",0,0);

To run this, save this code as, for instance, Replace.jsee, and then select this file from Select... in the Macros menu. Finally, select Run Replace.jsee in the Macros menu.

Explanations:

\J in the replacement expression specifies JavaScript. In this example, \2 is the backreference from (\d+), thus \2 / 2 represents a matched number divided by two.

References: EmEditor How to: Replace Expression Syntax



来源:https://stackoverflow.com/questions/64560449/trying-multiplying-numbers-on-a-line-starting-with-the-word-size-with-a-consta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!