我正在尝试将以下样式文本文件从 mm 转换为 m(即将所有数字除以 1000)。它由规则的图案组成,包含文本和数字。
我已经设法使用 python 解决了(最终),它有点粗糙并且已经准备好了,但它完成了任务。任何建议/改进将不胜感激。
import re
import numpy as np
import linecache
from io import StringIO
myfile = "file"
results = open("results.txt","w")
for i in range(1,50000):
line = linecache.getline(myfile,i)
if re.search('[a-zA-Z]', line):
results.write(line)
elif line.isspace():
results.write(str(line))
elif re.search('[-]', line):
results.write(str(line))
else:
c = StringIO(line)
data = np.loadtxt(c)
a = np.array(data)
c = a / 1000
d = str(c).replace('[','').replace(']','')
results.write(str(d)+'\n')
results.close()
文件是这样的:
add
tube
3033303.0 2998206.95111 106180.1625
60.325 6222.60621
y
0.0 0.0
0.0
add
cube
3027189.24332 3032175.78955 114508.75
168.9 6170.76909
y
0.0 0.0
0.0
期望的结果是;
add
tube
3033.3030 2998.20695111 106.1801625
0.060325 6.22260621
y
0.0 0.0
0.0
add
cube
3027.18924332 3032.17578955 114.50875
0.1689 6.17076909
y
0.0 0.0
0.0
请您参考如下方法:
简单的 Perl
perl -pe 's-(\d+.?\d*)-($1/1000)-ge' file
add
tube
3033.303 2998.20695111 106.1801625
0.060325 6.22260621
y
0 0
0
add
cube
3027.18924332 3032.17578955 114.50875
0.1689 6.17076909
y
0 0
0