0

I want to save this list in a CSV file in Python.

row = ['4CIIN', '04-11-2022', '00:00', '2022-11-05 00:00:00', '00:00', b'4CIIN Compiti Scrivi un programma C che, 3CIIN letto l\xe2\x80\x99anno di nascita di una persona e l\xe2\x80\x99anno attuale in input, calcoli l\xe2\x80\x99et\xc3\xa0 della persona. Restituisci un opportuno messaggio se la persona \xc3\xa8 maggiorenne o no.   - From Google', '0']

With this code:

with open('salva.csv','w', newline='\n', encoding='utf-8') as f:
    write=csv.writer(f)
    write.writerows(row)

My result seems not correct

4,C,I,I,N
0,4,-,1,1,-,2,0,2,2
0,0,:,0,0
2,0,2,2,-,1,1,-,0,5, ,0,0,:,0,0,:,0,0
0,0,:,0,0
52,67,73,73,78,32,67,111,109,112,105,116,105,32,83,99,114,105,118,105,32,117,110,32,112,114,111,103,114,97,109,109,97,32,67,32,99,104,101,44,32,51,67,73,73,78,32,108,101,116,116,111,32,108,226,128,153,97,110,110,111,32,100,105,32,110,97,115,99,105,116,97,32,100,105,32,117,110,97,32,112,101,114,115,111,110,97,32,101,32,108,226,128,153,97,110,110,111,32,97,116,116,117,97,108,101,32,105,110,32,105,110,112,117,116,44,32,99,97,108,99,111,108,105,32,108,226,128,153,101,116,195,160,32,100,101,108,108,97,32,112,101,114,115,111,110,97,46,32,82,101,115,116,105,116,117,105,115,99,105,32,117,110,32,111,112,112,111,114,116,117,110,111,32,109,101,115,115,97,103,103,105,111,32,115,101,32,108,97,32,112,101,114,115,111,110,97,32,195,168,32,109,97,103,103,105,111,114,101,110,110,101,32,111,32,110,111,46,32,32,32,45,32,70,114,111,109,32,71,111,111,103,108,101
0

I need some basics to how convert the string in a correct way, and add the delimiter.

3
  • 1
    Your list has a mix of strings and byte-strings... Commented Nov 1, 2022 at 0:58
  • 4
    In addition to the previous comment, use writerow for a single list, not writerows. Commented Nov 1, 2022 at 8:10
  • 1
    What seems incorrect about your results? What different output or format are you expecting? Commented Nov 1, 2022 at 16:21

1 Answer 1

1

One approach would be to first convert any bytes into utf-8. Then use .writerow() to write a single row.

When using a csv.writer() you should use newline='' to avoid extra unwanted newlines (this is explained in the Python docs).

For example:

import csv

row = ['4CIIN', '04-11-2022', '00:00', '2022-11-05 00:00:00', '00:00', b'4CIIN Compiti Scrivi un programma C che, 3CIIN letto l\xe2\x80\x99anno di nascita di una persona e l\xe2\x80\x99anno attuale in input, calcoli l\xe2\x80\x99et\xc3\xa0 della persona. Restituisci un opportuno messaggio se la persona \xc3\xa8 maggiorenne o no.   - From Google', '0']
row_str = [v.decode('utf-8') if type(v) == bytes else v for v in row]

with open('salva.csv','w', newline='', encoding='utf-8') as f_output:
    csv_output =  csv.writer(f_output)
    csv_output.writerow(row_str)

Giving you a single row with:

4CIIN,04-11-2022,00:00,2022-11-05 00:00:00,00:00,"4CIIN Compiti Scrivi un programma C che, 3CIIN letto l’anno di nascita di una persona e l’anno attuale in input, calcoli l’età della persona. Restituisci un opportuno messaggio se la persona è maggiorenne o no.   - From Google",0

Note: you will need to use an application that can correctly display utf-8 encodings to see this correctly.

Your code used .writerows(). This would try to iterate over each entry in your list and create a row for each. For example, it is iterating over your 4CIIN string as separate characters which is why you see individual letters in your output.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.