简体   繁体   中英

Insert next line in For loop

I'd like to have a next line inside my for loop, currently, what happens is that since it is inside a for loop, all the data is stored in an array and once I write it at the end of my code, it prints as one line.

fields = []
new_rows_list = []

file1 = open('CSV_sample_file.csv','rb')
reader = csv.reader(file1)

fields = reader.next()

for row in reader:
    for column in row:
        cellValue = column
        new_row = find_and_mask_cc(cellValue)
        new_rows_list.append(new_row)
file1.close()

file2 = open('CSV_sample_file2.csv', 'wb')
writer = csv.writer(file2)

writer.writerow(fields)
writer.writerow(new_rows_list)

file2.close()

What I am getting is this:

Created_User_ID__c,BRAND_Type__c,Migration_Remarks__c
EMSPDGBELNAS,test1,411111XXXXXX1111,EMSPDGCAJAPIN,test2,511111XXXXXX1111,EMSPDGNCRETES,test3,611111XXXXXX1111

My expected output is this:

Created_User_ID__c,BRAND_Type__c,Migration_Remarks__c
EMSPDGBELNAS,test1,411111XXXXXX1111
EMSPDGCAJAPIN,test2,511111XXXXXX111
EMSPDGNCRETES,test3,611111XXXXXX1111

You're appending all columns to the same list new_rows_list and writing it as one row with writer.writerow(new_rows_list) .

You can make new_rows_list a list of lists and use writer.writerows for output instead:

...

for row in reader:
    new_row = []
    for column in row:
        cellValue = column
        new_row.append(find_and_mask_cc(cellValue))
    new_rows_list.append(new_row)
file1.close()

file2 = open('CSV_sample_file2.csv', 'wb')
writer = csv.writer(file2)

writer.writerow(fields)
writer.writerows(new_rows_list)

...

Alternatively, you can pass to writerows a generator expression that iterates through reader to write each row with columns converted by find_and_mask_cc to the output as you read it from the input, so it won't require reading the entire input into memory:

with open('CSV_sample_file.csv') as file1, open('CSV_sample_file2.csv', 'w', newline='') as file2:
    reader = csv.reader(file1)
    writer = csv.writer(file2)
    writer.writerow(next(reader))
    writer.writerows(map(find_and_mask_cc, row) for row in reader)

Demo: https://repl.it/repls/SatisfiedSardonicExponents

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM