Te recomendamos que pruebes esta resolución en un ambiente controlado antes de pasarlo a producción, un saludo.
Ejemplo 1: crear un archivo csv python
# This action requires the 'csv' moduleimport 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:withopen('protagonist.csv','w', newline='')asfile:
writer = csv.writer(file)
writer.writerows(row_list)# This will create a csv file in the current directory
Ejemplo 2: escritura csv python
import csv
withopen('names.csv','w')as csvfile:
fieldnames =['first_name','last_name']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow('first_name':'Baked','last_name':'Beans')
writer.writerow('first_name':'Lovely','last_name':'Spam')
writer.writerow('first_name':'Wonderful','last_name':'Spam')
Ejemplo 3: lector de csv python
import csv
withopen('employee_birthday.txt', mode='r')as csv_file:
csv_reader = csv.DictReader(csv_file)
line_count =0for row in csv_reader:if line_count ==0:print(f'Column names are ", ".join(row)')
line_count +=1print(f'trow["name"] works in the row["department"] department, and was born in row["birthday month"].')
line_count +=1print(f'Processed line_count lines.')
Comentarios y calificaciones
Si posees algún recelo o forma de limar nuestro ensayo puedes escribir una crónica y con deseo lo observaremos.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)