2

Hi I am trying to get data from log file to extract specific data to control my progress.

I have written a code for open, read and write but I couldn't handle to get specific data.

import sys 
file=open(sys.argv[1],"r") 
file2=open('Data','a') if
file.mode == "r": 
contents =file.read()
print >> file2, contents

Depend to progress energy data is changing

for example for A progress energy count is 2500 but for B its energy count is 3000

output raw data

ETITLE:      TS           BOND          ANGLE          DIHED          IMPRP               ELECT            VDW       BOUNDARY           MISC        KINETIC               TOTAL           TEMP      POTENTIAL         TOTAL3        TEMPAVG

ENERGY:       0       263.6137       847.5817       263.7656        10.7299         -17411.2458      1948.8251      4767.8559         0.0000         0.0000          -9308.8739         0.0000     -9308.8739     -9308.8739         0.0000

ENERGY:       1       263.2889       846.9560       263.8744        10.9840         -17411.4025      1712.6659      4767.8559         0.0000         0.0000          -9545.7775         0.0000     -9545.7775     -9545.7775         0.0000

the data I want to get is the energy number and the potential energy.

example output I want to write to my Data:

Energy: 0 |Potential: -9308

Energy: 1 |Potential: -9508

How can I write a code to handle this situation ?

1
  • Is your output raw data, the format of the input log file or expected output structure? Commented Feb 20, 2019 at 18:53

2 Answers 2

1

I'd recommend going over these two resources which have to do with reading and writing to files:

https://automatetheboringstuff.com/chapter8/ and https://docs.python.org/2/tutorial/inputoutput.html

Specifically, if you are looking to only output Energy and Potential, I may recommend going line by line (info from the second link. That will make it easy to get through the log, so you can skip the information you don't care about.

To do this, use the readline() method, which will return your log line by line:

with open(newfile, 'w') as outfile:
    l = f.readline()
    print(l)
    # do stuff with l 

That should make it easier to grab the content you care about and write it to the file.

Hope that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

hi thanks for tutorial files I'm quite new to python. our professor said we need to learn it for data analysis, I was looking for such tutorial
0

What format is your datafile?

If it's a spreadsheet you might consider using a library like https://openpyxl.readthedocs.io/en/stable/tutorial.html#accessing-many-cells to help make accessing specific rows/columns easier.

If you're just reading in each line like that, you could split it each line by space/tab and then access cols[13] to get the Potential.

# Read in file
file=open("input.txt")
lines = file.readlines()
output = []
for line in lines:
    # Split on whitespace
    cols = line.split()
    # Skip empty lines
    if len(cols) == 0:
        continue
    # Manually figure out that potential is the 13th column (starting from 0)
    output.append("Energy: " + str(cols[1]) + "|Potential: " + str(cols[13]))

file2=open("output.txt",'a')
file2.write("\n".join(output))

1 Comment

hi, this is original file of data drive.google.com/open?id=1TU-uGvhtSbrEjRwfip5bbqIrc01LqjKf also thanks for example code for get data, I have tried to edit it also I have followed tutorial but my attemps resulted with empty output file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.