简体   繁体   中英

Add row if len(row) == 2 (python, csv)

I would like to generate a new row when there are only 2 rows

with open(pathCSV) as f:
     reader = csv.reader(f)
     for row in reader:                     
         if len(row) == 2:
            #add row

if i print my row it return me that:

['Contrôle', '']
['CLÉS', '']
['Entrée', '4 clés entrées']
['Entrée 2', '1 clé entrée porte bâtiment']

and i want to have:

['Contrôle', '','']
['CLÉS', '','']
['Entrée', '4 clés entrées','']
['Entrée 2', '1 clé entrée porte bâtiment','']

Just appending a new value should work.

with open(pathCSV) as f:
     reader = csv.reader(f)
     for row in reader:                     
         if len(row) == 2:
             row.append('')
             print(row)

returns

['Contrôle', '','']
['CLÉS', '','']
['Entrée', '4 clés entrées','']
['Entrée 2', '1 clé entrée porte bâtiment','']

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