문제

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

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top