python - Unit conversion from mm to m in text file with text and numbers -
i'm trying convert following style text file mm m (i.e., divide numbers 1000). it's made of regular pattern , contains text numbers.
i have managed solve (eventually) using python, it's bit rough , ready completes task. suggestions/improvements appreciated.
import re import numpy np import linecache io import stringio myfile = "file" results = open("results.txt","w") 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) = np.array(data) c = / 1000 d = str(c).replace('[','').replace(']','') results.write(str(d)+'\n') results.close()
the file so:
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
desired outcome be;
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
easy in 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
Comments
Post a Comment