Answers for "create csv from python"

16

create csv file python

# This action requires the 'csv' module
import csv

# The basic usage is to first define the rows of the csv file:
row_list = [["SN", "Name", "Contribution"],
             [1, "Linus Torvalds", "Linux Kernel"],
             [2, "Tim Berners-Lee", "World Wide Web"],
             [3, "Guido van Rossum", "Python Programming"]]

# And then use the following to create the csv file:
with open('protagonist.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(row_list)
# This will create a csv file in the current directory
Posted by: Guest on February-24-2020
5

python csv dict reader

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
...
Eric Idle
John Cleese

>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}
Posted by: Guest on August-13-2020
1

csv reader python

import csv

with open('employee_birthday.txt', mode='r') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    line_count = 0
    for row in csv_reader:
        if line_count == 0:
            print(f'Column names are {", ".join(row)}')
            line_count += 1
        print(f't{row["name"]} works in the {row["department"]} department, and was born in {row["birthday month"]}.')
        line_count += 1
    print(f'Processed {line_count} lines.')
Posted by: Guest on May-15-2020
0

créer csv python

#!/usr/bin/env python
# -*-coding: utf-8 -*

entetes = [
     u'Colonne1',
     u'Colonne2',
     u'Colonne3',
     u'Colonne4',
     u'Colonne5'
]

valeurs = [
     [u'Valeur1', u'Valeur2', u'Valeur3', u'Valeur4', u'Valeur5'],
     [u'Valeur6', u'Valeur7', u'Valeur8', u'Valeur9', u'Valeur10'],
     [u'Valeur11', u'Valeur12', u'Valeur13', u'Valeur14', u'Valeur15']
]

f = open('monFichier.csv', 'w')
ligneEntete = ";".join(entetes) + "n"
f.write(ligneEntete)
for valeur in valeurs:
     ligne = ";".join(valeur) + "n"
     f.write(ligne)

f.close()
Posted by: Guest on August-19-2021

Code answers related to "create csv from python"

Python Answers by Framework

Browse Popular Code Answers by Language