1

I would like to input or read the following data of a .txt file:

VIBRATION/SHOCK CALIBRATION DATA
DATE: 2/26/2012 
TIME: 0800
Time (ms)   Channel 1    Channel 2     Channel 3
0.0         -0.9         9.0           12.9
50.0        5.0          12            343  
100.0       56.7         120           0.8
150.0       90.0         0.9           12.0
200.0       32.9         12.4          34.0

Then output to a new .txt file such that only the numbers are written for Time and Channel 3 columns:

0.0     12.9
50.0    343
100.0   0.8
150.0   12.0
200.0   34.0
1
  • I have used a counter to only read below Time then output to a file but don't know the command to extract only Channel 3. Commented Feb 28, 2012 at 17:29

3 Answers 3

3

As a complete example, consider something like the code below. I've added excessive comments to explain what each step is doing.

# Open the input file read-only...
with open('infile.txt', 'r') as infile:
    # Skip the first 4 lines, and store them as "header" in case we need them...
    # It's important that we use "next" here and _not_ infile.readline().
    # readline and the file iteration methods ("for line in file") can't be mixed
    header = [next(infile) for dummy in range(4)]

    # Open the output file, overwriting whatever is there...
    with open('outfile.txt', 'w') as outfile:
        # Loop over the lines in the input file
        for line in infile:
            # Strip off leading and trailing whitespace (e.g "\n") and 
            # split on whitespace. This gives us a list of strings.
            columns = line.strip().split()
            # Write the 1st and 4th columns in each row as left-justified 
            # columns with a fixed-width of 8
            outfile.write('{:8}{:8}\n'.format(columns[0], columns[3]))

If you're using an old version of python and wanted to avoid the with statements, you could write it like this:

# Open the input file read-only...
infile = open('infile.txt', 'r')
# Open the output file, overwriting whatever is there...
outfile = open('outfile.txt', 'w')

# Skip the first 4 lines, and store them as "header" in case we need them...
# It's important that we use "next" here and _not_ infile.readline().
# readline and the file iteration methods ("for line in file") can't be mixed
header = [next(infile) for dummy in range(4)]

# Loop over the lines in the input file
for line in infile:
    # Strip off leading and trailing whitespace (e.g "\n") and 
    # split on whitespace. This gives us a list of strings.
    columns = line.strip().split()
    # Write the 1st and 4th columns in each row as left-justified 
    # columns with a fixed-width of 8
    outfile.write('{:8}{:8}\n'.format(columns[0], columns[3]))

# Close the file objects once we're through with them..
# Python would close these automatically when the process ends, but it's a good
# idea to get in the habit of explicitly closing them once you don't need them.
# Otherwise, when you start writing code for longer-running processes, you'll 
# inadvertently leave lots of file handles laying around
infile.close()
outfile.close()

However, it's a good idea to get into the habit of using with statements to handle file objects. They ensure that file handles will be closed automatically even if there's an error in your code. with statements are "context managers". They're very handy for a lot of things that would otherwise require "boilerplate" cleanup and/or entry code.

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

2 Comments

I'd add that you should be using the rb and wb (read and write binary, respectively), as it will have fewer cross-platform issues than the default r and w modes.
Thanks for the feedback. Makes much more sense.
0

You can call split() on the line string to get the columns:

t, ch1, ch2, ch3 = line.split()

The values returned will be strings. If you want numbers, use float() to convert them:

t_f = float(t)
ch3_f = float(ch3)

Comments

0

I'm not really a python guy but building on your comment above, once you've read down to the data you should be able to use str.split() on each line to get the values of the columns in that line.

Documentation here: http://docs.python.org/release/3.1.3/library/stdtypes.html#string-methods

Comments

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.