Hi,

Firstly, I am quite new to python so hopefully this could be quick answer for any kind sole who give help.

I am trying to connect the data files created from one piece of equipment to another process that require similar, but not the same, data .txt files.

The first data file is generated to me in this format (sample data file attached below);

#sensor head correlation#
set initialised 0 0 0
set time division 50
set table dimension X mm 500
set table dimension Y mm 500
sensor head #1
T X Y
1 32 245
2 33.0568 244.167
3 33.7287 243.388
4 34.6067 242.538
5 35.4018 241.787
6 35.9536 241.022
.......etc,

And then I need to reformat this into a format like this ;

32 245
33.0568 244.167
33.7287 243.388
34.6067 242.538
35.4018 241.787
35.9536 241.022
...etc,


I can extract the data ok but I can't seem to format the final output as above, I either come out with ;

('32', '245')
('33.0568', '244.167')
('33.7287', '243.388')
('34.6067', '242.538')
('35.4018', '241.787')
('35.9536', '241.022')

or

32 245 33.0568 244.167 33.7287 243.388 34.6067 242.538 35.4018 241.787 35.9536 241.022


Nomatter which combinations of .write formatting or regex expression I have adjusted.

Script so far and datafile attached. I am so close to what I need so a massive thanks to any experienced python guy who can heeeeeelp me!!

Cheers,
Frank

Recommended Answers

All 3 Replies

My suggestion:

#! /usr/bin/python

import sys, os, string, re

# input/output for filenames
filename = 'data.txt'
newfilename = 'data_out.txt'

# read file
if os.path.exists(filename):
    data = open(filename,'r')
    dataInput = data.read()
else:
    print "File not found."
    raise SystemExit
numPattern = r'\s+([\+\-]?\d*\.?\d*)'
r = re.compile(r'''
    ^\s*                         # Skip whitespace at start of line
    %s%s%s                       # Three numbers, each preceded by whitespace
    \s*$                         # Optional whitespace, and end of line
    ''' % (numPattern, numPattern, numPattern),
    re.VERBOSE | re.MULTILINE)
results = '\n'.join(' '.join(line[1:]) for line in r.findall(dataInput))
print results

# function to write file
def writefile(newfilename):
    open(newfilename, 'w').write(results)
    print "File written."

# function check overwrite existing of file
def overwrite_ok(newfilename):
    while True:
        response = raw_input("Are you sure you want to overwrite "+str(newfilename)+"? Yes or No\n")
        if response == "Yes":
            writefile(newfilename)
            return
        elif response == "No":
            print "Aborted."
            return
        else:
            print "Please enter Yes or No."

# write/overwrite
if os.path.exists(newfilename):
    overwrite_ok(newfilename)
else:
    writefile(newfilename)

Pay attention to line 23 of tonyjv+ code.

That is the solution to why your data was not what you wanted.

Goodluck
:)

Hi,

Aaah! I see. Easy when you know how, huh? Anywho, THANKS a million for 'dragging me over the finish line!' If ever I can repay the favour ..

Happy holidays!!
Frank

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.