Robert A.  Fettikowski
Robert A. Fettikowski

Reputation: 185

Writing only part of a line to a file

I want to clean up my output and only write part of the line that I need to a new file and not the whole entire line. This is the relevent coding section:

counter = 1

for line in completedataset:
    print counter
    counter +=1

    for t in matchedLines:
       if t in line[:line.find(',')]:
            smallerdataset.write(line)
            break

Here is a sample of the data:

NOVE1780418","---","JAX17054099","5","156323558", etc for the line.

I only want to write up until the number before the 3rd comma. I need some help modifying write line to only write up until the third comma. This file is very large and I'm hoping that any new code won't slow the program down but rather speed it up. Thanks Bob

Upvotes: 3

Views: 2762

Answers (2)

John La Rooy
John La Rooy

Reputation: 304355

for line in infile:
    line = line.strip().split(',',3)
    outfile.write(','.join(line[:-1]) + '\n')

If there is a possibility of ',' showing up in any of the fields, you'll need to use the csv module

Upvotes: 1

Joe Kington
Joe Kington

Reputation: 284770

It should be as simple as this...

for line in infile:
    line = line.strip().split(',')
    outfile.write(','.join(line[:3]) + '\n')

Upvotes: 4

Related Questions