Choosing only non-zeros from a long list of numbers in text file

2024/10/6 9:28:05

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 = ik = re.search(r'([- ]\d+\.\d+)+' , d)if k:r1.append(k.group())file.write(str(' '.join(map(str,r1)))) 
file.close() 
Answer

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())
https://en.xdnf.cn/q/120070.html

Related Q&A

Why can I not plot using Python on repl.it

For practical reasons, I want to test a small piece of Pyton code on repl.it (webbased, so I do not need to install Python).The codeimport numpy as np import matplotlib.pyplot as plttime = np.array([0,…

Pull Data from web link to Dataframe [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

Python program that rolls a fair die counts the number of rolls before a 6 shows up

import randomsample_size = int(input("Enter the number of times you want me to roll the die: "))if (sample_size <=0):print("Please enter a positive number!")else:counter1 = 0coun…

How to read CSV file in Python? [duplicate]

This question already has answers here:How do I read and write CSV files?(9 answers)Closed 1 year ago.Im using Spyder for Python 2.7 on Windows 8. Im trying to open and read a csv file and see all the…

How to match the bundle id for android app?

Id like to match the urls like this:input: x = "https://play.google.com/store/apps/details?id=com.alibaba.aliexpresshd&hl=en"get_id(x)output: com.alibaba.aliexpresshdWhat is the best way…

Memory Usage During running a Deep learning CNN Model in Colab

I am conducting a research which requires me to know the memory used during run time by the model when i run a deep learning model(CNN) in google colab. Is there any code i can use to know the same .Ba…

Gensim example, TypeError:between str and int error

When running the below code. this Python 3.6, latest Gensim library in Jupyterfor model in models:print(str(model))pprint(model.docvecs.most_similar(positive=["Machine learning"], topn=20))[1…

How to interpret this JSON file?

Im trying to interpret this JSON file but I couldnt figure it out. {"results": [{"fsq_id": "4dc586fbcc3ff3b3045e2ef3","categories": [{"id": 17056,"…

How to extract a field from this payload with a regex? [duplicate]

This question already has answers here:Parse JSON with Python(2 answers)Closed 6 years ago.I have this payload that I wish to extract a field from:{"encrypted_sender_transaction_id":"514…

Python reading xml

I am newbie on Python programming. I have requirement where I need to read the xml structure and build the new soap request xml by adding namespace like here is the example what I have Below XML which …