2019-08-28

Python - List of lists to CSV

import csv

# Data
headers = ['Header1', 'Header2', 'Header3']
rows = [[1,2,3], [5,6,7]]

# Open a file to append to
with open('/path/target_file.txt', 'a') as f:

    # CSV writer instantiation
    writer = csv.writer(
    f
    # Settings
    , quoting=csv.QUOTE_NONE
    , delimiter='\t'
    )

    # Headers
    writer.writerow(headers)

    # All rows
    writer.writerows(rows)

No comments:

Post a Comment