0

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
ancriz
  • 3
  • 2
  • 1
    Can you add "\n" character to the end of the row you are writing? Something like this: https://stackoverflow.com/questions/2918362/writing-string-to-a-file-on-a-new-line-every-time – Richard K Yu Sep 13 '19 at 15:44
  • You can pass delimeter. e.g open('CSV_sample_file2.csv', 'wb', delimiter='\n') – Mirza715 Sep 13 '19 at 15:46

1 Answers1

1

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

blhsing
  • 91,368
  • 6
  • 71
  • 106
  • Hi thanks for the help, I got something like this: Created_User_ID__c,BRAND_Type__c,Migration_Remarks__c "['EMSPDGBELNAS', 'test1', '411111XXXXXX1111']","['EMSPDGCAJAPIN', 'test2', '511111XXXXXX1111']","['EMSPDGNCRETES', 'test3', '611111XXXXXX1111']" There are 3 arrays now, which is good, but how can I display it like this? Thank you very much in advance! You're a genius! Created_User_ID__c,BRAND_Type__c,Migration_Remarks__c EMSPDGBELNAS,test1,411111XXXXXX1111 EMSPDGCAJAPIN,test2,511111XXXXXX111 EMSPDGNCRETES,test3,611111XXXXXX1111 (kindly refer to my expected output) thanks again – ancriz Sep 13 '19 at 15:55
  • There are 3 arrays now, which is good, but how can I display it similar to my expected output? Thank you so much in advance! – ancriz Sep 13 '19 at 16:00
  • Please update your question with the output you're getting properly formatted so that I can better help. – blhsing Sep 13 '19 at 16:01
  • Hi blhsing! Great code, this is awesome, you did everything in few lines of code. Will study the code, thank you very much! By the way, for the regex part, can you also include numbers with spaces? currently, it only masks numbers without spaces, i.e., 4111111111111111, and not this one 4111 1111 1111 1111. I would appreciate all your help, sorry I am new to Python. – ancriz Sep 15 '19 at 17:13