开发者

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 wa开发者_运维技巧nt 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


It should be as simple as this...

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜