从文本文件中的一长串数字中仅选择非零 [英] Choosing only non-zeros from a long list of numbers in text file
本文介绍了从文本文件中的一长串数字中仅选择非零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有一长串数字的文本文件.我只想选择非零值并制作另一个文本文件.
I have a text file with a long list of numbers. I would like to choose only the non-zeros and make another text file.
这是输入文件的一部分:
This is a portion of the input file:
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 5.16677E-30
9.61708E-28 1.18779E-25 9.73432E-24 5.29352E-22 1.91009E-20 4.57336E-19
7.26588E-18 7.65971E-17 5.35806E-16 2.48699E-15 7.65973E-15 1.56539E-14
2.12278E-14 1.91010E-14 1.14046E-14 4.51832E-15 1.18780E-15 2.07196E-16
2.39824E-17 1.84193E-18 9.38698E-20 3.17431E-21 7.12271E-23 1.06050E-24
1.04773E-26 6.86848E-29 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
上面显示的输入部分的预期输出为:
The expected out put for the portion of the input show above would be:
5.16677E-30 9.61708E-28 1.18779E-25 9.73432E-24 5.29352E-22 1.91009E-20
4.57336E-19 7.26588E-18 7.65971E-17 5.35806E-16 2.48699E-15 7.65973E-15
1.56539E-14 2.12278E-14 1.91010E-14 1.14046E-14 4.51832E-15 1.18780E-15
2.07196E-16 2.39824E-17 1.84193E-18 9.38698E-20 3.17431E-21 7.12271E-23
1.06050E-24 1.04773E-26
我尝试了下面的内容,但未返回任何内容.
I tried what I wrote below but it is not returning anything.
r1=[]
file = open ('aa2','w')
with open('aa.txt') as m:
file.write('List')
file.write("\n")
for t in itertools.islice(m,500,6500):
for i in t:
if i != 0.00000E+00 :
d = i
k = re.search(r'([- ]\d+\.\d+)+' , d)
if k:
r1.append(k.group())
file.write(str(' '.join(map(str,r1))))
file.close()
推荐答案
您在不需要的地方再次使用了regex.在文件上使用islice
的地方,您所做的事情也异常诡异.这也是不必要的.您可以这样做:
You're using regex again where you don't need to. You're also doing something exceedingly bizarre where you're using islice
on the file. That's also unnecessary. You could just do this:
import io
file = io.StringIO('''
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 5.16677E-30
9.61708E-28 1.18779E-25 9.73432E-24 5.29352E-22 1.91009E-20 4.57336E-19
7.26588E-18 7.65971E-17 5.35806E-16 2.48699E-15 7.65973E-15 1.56539E-14
2.12278E-14 1.91010E-14 1.14046E-14 4.51832E-15 1.18780E-15 2.07196E-16
2.39824E-17 1.84193E-18 9.38698E-20 3.17431E-21 7.12271E-23 1.06050E-24
1.04773E-26 6.86848E-29 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00
'''.strip())
#################################
# Actual Answer to your problem #
#################################
values = []
for line in file:
values.extend(val for val in line.strip().split() if val != '0.00000E+00')
with io.StringIO() as out:
for i, val in enumerate(values):
if i and not i % 6:
out.write('\n')
out.write(val+' ')
out.seek(0)
print(out.read())
这篇关于从文本文件中的一长串数字中仅选择非零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文