将文本文件中的所有数字除以 2
Divide all numbers in a text file by 2
我一直在尝试将文本文件中的所有数字与单词分开,然后通过 excel 中的公式 运行 它们并使用类似于 [=16] 的方法批量替换它们=] 在 Notepad++ 中。
在我的情况下,这不起作用,因为在整个文本中出现重复出现的数字,因此无论我尝试做什么,搜索和替换都会陷入无限循环。尤其是涉及到小数和整数的时候,Notepad++无法在逻辑上区分1.14、1和14,所以会变得很乱。
有什么方法可以直接在 Notepad++ 或网上的某个地方执行此操作吗?或者,是否有一种方法可以从文本中提取数字,在文本中保留它们的 space,能够编辑所有值,然后将它们重新插入文档中的原始位置?
编辑:
我要转:
<OptionNumeric name='vnlaClayFreq' default='1' min='0' max='5' </OptionNumeric>
<Setting name='MotherlodeSize' avg=':= 1.648 * _default_ * vnlaClaySize ' range=':= 1.648 * _default_ * vnlaClaySize ' type='normal' />
<Setting name='MotherlodeHeight' avg=':= 64 ' range=':= 10 ' type='uniform' scaleTo='seaLevel' />
进入:
<OptionNumeric name='vnlaClayFreq' default='1' min='0' max='3' </OptionNumeric>
<Setting name='MotherlodeSize' avg=':= 0.824 * _default_ * vnlaClaySize ' range=':= 0.824 * _default_ * vnlaClaySize ' type='normal' />
<Setting name='MotherlodeHeight' avg=':= 32 ' range=':= 5 ' type='uniform' scaleTo='seaLevel' />
其中整数仍然是整数,必要时四舍五入到最接近的整数(即 1 仍然是 1),而小数会像 Toto 的 python 代码那样被改变(即 1.0 变成 0.5)。
您可以在 PythonScript 插件中 运行 python 脚本。
如果还没有安装,按照这个guide
创建脚本(插件 >> PythonScript >> 新脚本)
复制此代码并保存文件(例如calculate.py
):
import re # regular expression package
import math # math functions package
# define function calculate
def calculate(match):
# if group 2 exists (it is an integer)
if (match.group(2)):
# round up (ceil) the group 2 divided by 2 and return an integer
res = int(math.ceil(float(match.group(2))/2))
else: # group 2 doesn't exist, so process group 1 (float)
# divide group 1 by 2
res = float(match.group(1))/2
# return the result of the calculation
return res
# replace in editor window what is matched by regex by a call to calculate function
editor.rereplace('(\d+\.\d+)|(\d+)', calculate)
# regex
# (\d+\.\d+) : group 1, matches a float
# | : OR (regex operator)
# (\d+) : group 2, matches an integer
- 打开您要更改的文件
- 运行 脚本(插件 >> PythonScript >> 脚本 >> 计算)
- 完成
屏幕截图(之前):
截图(后):
我一直在尝试将文本文件中的所有数字与单词分开,然后通过 excel 中的公式 运行 它们并使用类似于 [=16] 的方法批量替换它们=] 在 Notepad++ 中。
在我的情况下,这不起作用,因为在整个文本中出现重复出现的数字,因此无论我尝试做什么,搜索和替换都会陷入无限循环。尤其是涉及到小数和整数的时候,Notepad++无法在逻辑上区分1.14、1和14,所以会变得很乱。
有什么方法可以直接在 Notepad++ 或网上的某个地方执行此操作吗?或者,是否有一种方法可以从文本中提取数字,在文本中保留它们的 space,能够编辑所有值,然后将它们重新插入文档中的原始位置?
编辑:
我要转:
<OptionNumeric name='vnlaClayFreq' default='1' min='0' max='5' </OptionNumeric>
<Setting name='MotherlodeSize' avg=':= 1.648 * _default_ * vnlaClaySize ' range=':= 1.648 * _default_ * vnlaClaySize ' type='normal' />
<Setting name='MotherlodeHeight' avg=':= 64 ' range=':= 10 ' type='uniform' scaleTo='seaLevel' />
进入:
<OptionNumeric name='vnlaClayFreq' default='1' min='0' max='3' </OptionNumeric>
<Setting name='MotherlodeSize' avg=':= 0.824 * _default_ * vnlaClaySize ' range=':= 0.824 * _default_ * vnlaClaySize ' type='normal' />
<Setting name='MotherlodeHeight' avg=':= 32 ' range=':= 5 ' type='uniform' scaleTo='seaLevel' />
其中整数仍然是整数,必要时四舍五入到最接近的整数(即 1 仍然是 1),而小数会像 Toto 的 python 代码那样被改变(即 1.0 变成 0.5)。
您可以在 PythonScript 插件中 运行 python 脚本。
如果还没有安装,按照这个guide
创建脚本(插件 >> PythonScript >> 新脚本)
复制此代码并保存文件(例如calculate.py
):
import re # regular expression package
import math # math functions package
# define function calculate
def calculate(match):
# if group 2 exists (it is an integer)
if (match.group(2)):
# round up (ceil) the group 2 divided by 2 and return an integer
res = int(math.ceil(float(match.group(2))/2))
else: # group 2 doesn't exist, so process group 1 (float)
# divide group 1 by 2
res = float(match.group(1))/2
# return the result of the calculation
return res
# replace in editor window what is matched by regex by a call to calculate function
editor.rereplace('(\d+\.\d+)|(\d+)', calculate)
# regex
# (\d+\.\d+) : group 1, matches a float
# | : OR (regex operator)
# (\d+) : group 2, matches an integer
- 打开您要更改的文件
- 运行 脚本(插件 >> PythonScript >> 脚本 >> 计算)
- 完成
屏幕截图(之前):
截图(后):