Importación de archivos CSV — 8:17 min

  • 8:17 min | Última modificación: Octubre 13, 2021 | YouTube

[1]:
%%writefile /tmp/data.csv
Name,Address,City,ZipCode,Date,Score
Isabelle I. Dotson,"P.O. Box 477, 7357 Cras St.",LamontzŽe,12144-86218,02.24.21,21
Erin F. Munoz,"P.O. Box 737, 6015 Ligula St.",Springfield,6373,10.15.20,21
Troy G. Harrell,3706 Ut St.,Herne,69259,06.28.21,23
Yen W. Summers,"P.O. Box 939, 8016 Egestas Av.",Elversele,93452,05.24.21,21
Jerome B. Carney,298-7004 Natoque St.,Osimo,20619,09.23.20,24
Yuri O. Head,"P.O. Box 214, 231 Dapibus Ave",Lac-Serent,4464,12.22.21,17
Ima A. Richard,"Ap #496-7181 Ullamcorper, St.",Lehrte,21449,09.12.21,22
Eleanor Q. Guerrero,5425 Ornare St.,Arendonk,00636,02.15.22,21
Stuart F. Daniels,719-8478 Nunc St.,Mataram,43168,06.20.21,15
Cara G. Beach,648-517 Velit. Av.,Casciana Terme,78450,02.12.22,19
Nolan E. Cortez,Ap #766-6998 Gravida Rd.,Manfredonia,09534,12.14.20,23
Darius H. Beach,Ap #251-7151 Donec Av.,Valuyki,903927,10.31.21,14
Christian T. Mercer,"P.O. Box 858, 1997 Elit, Avenue",Guaymas,21574,10.20.20,19
Florence F. Gordon,543-831 Est. Rd.,Valcourt,6387,12.01.21,25
Wilma I. Patton,"P.O. Box 299, 7144 Orci Street",Palermo,233311,12.27.20,17
Rajah I. Jensen,Ap #782-6146 Turpis St.,Arquata del Tronto,15936,02.06.21,21
Zoe U. Whitfield,Ap #200-7243 Sit St.,Chesapeake,W2A 0QO,12.01.21,19
May K. Wood,"777-1067 Auctor, Ave",Maastricht,754025,01.06.21,13
Beck Q. Monroe,"P.O. Box 123, 9085 Lorem, Rd.",Reading,27665,08.03.22,24
Amanda A. Marks,638-3732 Fermentum Road,Sindelfingen,678968,11.20.20,11
Serina X. Lang,"P.O. Box 780, 2133 Rutrum Road",Bromyard,Z7366,07.02.21,14
Lionel L. Bartlett,Ap #400-1963 Pellentesque. Ave,Aparecida de Goiânia,744476,12.21.21,17
Florence Z. Oconnor,839 Cras Road,Etawah,15197,11.29.20,24
Chloe Y. Sears,361-5600 Per St.,Mission,0787 KM,08.08.22,19
Mariam N. Tyler,589-6397 Scelerisque Road,Pozzuolo del Friuli,T5G 6M8,05.12.21,23
Dana A. Jefferson,Ap #142-8392 Consequat Avenue,Khammam,49399,02.06.21,20
Nola N. Chan,Ap #878-314 Faucibus Rd.,Requínoa,1640,06.10.22,10
Owen Z. Odom,1659 Risus. Street,Valuyki,12758,02.03.21,20
Serina B. Nash,"P.O. Box 143, 5197 At Avenue",San Costantino Calabro,77234,10.31.21,18
Salvador I. Powell,356-7423 Semper Road,Cardiff,Z4197,05.28.22,19
Writing /tmp/data.csv

Python csv API

[2]:
import csv

dir(csv)
[2]:
['Dialect',
 'DictReader',
 'DictWriter',
 'Error',
 'OrderedDict',
 'QUOTE_ALL',
 'QUOTE_MINIMAL',
 'QUOTE_NONE',
 'QUOTE_NONNUMERIC',
 'Sniffer',
 'StringIO',
 '_Dialect',
 '__all__',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__spec__',
 '__version__',
 'excel',
 'excel_tab',
 'field_size_limit',
 'get_dialect',
 'list_dialects',
 're',
 'reader',
 'register_dialect',
 'unix_dialect',
 'unregister_dialect',
 'writer']
[3]:
#
# Archivo delimitado por comas con reader()
# =============================================================================
#
with open("/tmp/data.csv", "r") as csv_file:

    #
    # Retorna un objeto reader que puede ser
    # usado para iterar sobre las lineas del
    # archivo
    #
    csv_reader = csv.reader(
        csv_file,
        delimiter=",",
        quotechar='"',
    )

    #
    # El for debe estar dentro del ambito
    # del bloque with
    #
    for row in csv_reader:
        print(row)
['Name', 'Address', 'City', 'ZipCode', 'Date', 'Score']
['Isabelle I. Dotson', 'P.O. Box 477, 7357 Cras St.', 'LamontzŽe', '12144-86218', '02.24.21', '21']
['Erin F. Munoz', 'P.O. Box 737, 6015 Ligula St.', 'Springfield', '6373', '10.15.20', '21']
['Troy G. Harrell', '3706 Ut St.', 'Herne', '69259', '06.28.21', '23']
['Yen W. Summers', 'P.O. Box 939, 8016 Egestas Av.', 'Elversele', '93452', '05.24.21', '21']
['Jerome B. Carney', '298-7004 Natoque St.', 'Osimo', '20619', '09.23.20', '24']
['Yuri O. Head', 'P.O. Box 214, 231 Dapibus Ave', 'Lac-Serent', '4464', '12.22.21', '17']
['Ima A. Richard', 'Ap #496-7181 Ullamcorper, St.', 'Lehrte', '21449', '09.12.21', '22']
['Eleanor Q. Guerrero', '5425 Ornare St.', 'Arendonk', '00636', '02.15.22', '21']
['Stuart F. Daniels', '719-8478 Nunc St.', 'Mataram', '43168', '06.20.21', '15']
['Cara G. Beach', '648-517 Velit. Av.', 'Casciana Terme', '78450', '02.12.22', '19']
['Nolan E. Cortez', 'Ap #766-6998 Gravida Rd.', 'Manfredonia', '09534', '12.14.20', '23']
['Darius H. Beach', 'Ap #251-7151 Donec Av.', 'Valuyki', '903927', '10.31.21', '14']
['Christian T. Mercer', 'P.O. Box 858, 1997 Elit, Avenue', 'Guaymas', '21574', '10.20.20', '19']
['Florence F. Gordon', '543-831 Est. Rd.', 'Valcourt', '6387', '12.01.21', '25']
['Wilma I. Patton', 'P.O. Box 299, 7144 Orci Street', 'Palermo', '233311', '12.27.20', '17']
['Rajah I. Jensen', 'Ap #782-6146 Turpis St.', 'Arquata del Tronto', '15936', '02.06.21', '21']
['Zoe U. Whitfield', 'Ap #200-7243 Sit St.', 'Chesapeake', 'W2A 0QO', '12.01.21', '19']
['May K. Wood', '777-1067 Auctor, Ave', 'Maastricht', '754025', '01.06.21', '13']
['Beck Q. Monroe', 'P.O. Box 123, 9085 Lorem, Rd.', 'Reading', '27665', '08.03.22', '24']
['Amanda A. Marks', '638-3732 Fermentum Road', 'Sindelfingen', '678968', '11.20.20', '11']
['Serina X. Lang', 'P.O. Box 780, 2133 Rutrum Road', 'Bromyard', 'Z7366', '07.02.21', '14']
['Lionel L. Bartlett', 'Ap #400-1963 Pellentesque. Ave', 'Aparecida de Goiânia', '744476', '12.21.21', '17']
['Florence Z. Oconnor', '839 Cras Road', 'Etawah', '15197', '11.29.20', '24']
['Chloe Y. Sears', '361-5600 Per St.', 'Mission', '0787 KM', '08.08.22', '19']
['Mariam N. Tyler', '589-6397 Scelerisque Road', 'Pozzuolo del Friuli', 'T5G 6M8', '05.12.21', '23']
['Dana A. Jefferson', 'Ap #142-8392 Consequat Avenue', 'Khammam', '49399', '02.06.21', '20']
['Nola N. Chan', 'Ap #878-314 Faucibus Rd.', 'Requínoa', '1640', '06.10.22', '10']
['Owen Z. Odom', '1659 Risus. Street', 'Valuyki', '12758', '02.03.21', '20']
['Serina B. Nash', 'P.O. Box 143, 5197 At Avenue', 'San Costantino Calabro', '77234', '10.31.21', '18']
['Salvador I. Powell', '356-7423 Semper Road', 'Cardiff', 'Z4197', '05.28.22', '19']
[4]:
#
# Archivo delimitado por comas con DictReader()
# =============================================================================
#
fieldnames = [
    "Name",
    "Address",
    "City",
    "ZipCode",
    "Date",
    "Score",
]

with open("/tmp/data.csv", "r") as csv_file:

    csv_dict_reader = csv.DictReader(
        csv_file,
        delimiter=",",
        quotechar='"',
        fieldnames=fieldnames,
    )

    #
    # Note que en este caso el archivo no deberia
    # tener encabezamiento o se puede descartar
    # la primera iteración en el codigo
    #
    for row in csv_dict_reader:
        print(row["Name"] + " ----> " + row["Score"])
Name ----> Score
Isabelle I. Dotson ----> 21
Erin F. Munoz ----> 21
Troy G. Harrell ----> 23
Yen W. Summers ----> 21
Jerome B. Carney ----> 24
Yuri O. Head ----> 17
Ima A. Richard ----> 22
Eleanor Q. Guerrero ----> 21
Stuart F. Daniels ----> 15
Cara G. Beach ----> 19
Nolan E. Cortez ----> 23
Darius H. Beach ----> 14
Christian T. Mercer ----> 19
Florence F. Gordon ----> 25
Wilma I. Patton ----> 17
Rajah I. Jensen ----> 21
Zoe U. Whitfield ----> 19
May K. Wood ----> 13
Beck Q. Monroe ----> 24
Amanda A. Marks ----> 11
Serina X. Lang ----> 14
Lionel L. Bartlett ----> 17
Florence Z. Oconnor ----> 24
Chloe Y. Sears ----> 19
Mariam N. Tyler ----> 23
Dana A. Jefferson ----> 20
Nola N. Chan ----> 10
Owen Z. Odom ----> 20
Serina B. Nash ----> 18
Salvador I. Powell ----> 19
[5]:
#
# NamedTuple
# =============================================================================
#
from collections import namedtuple

fieldnames = namedtuple(
    "Record",
    "Name, Address, City, ZipCode, Date, Score",
)

with open("/tmp/data.csv", "r") as csv_file:

    csv_reader = csv.reader(
        csv_file,
        delimiter=",",
        quotechar='"',
    )

    for row in map(fieldnames._make, csv_reader):
        print(row.Name + " ----> " + row.Score)
Name ----> Score
Isabelle I. Dotson ----> 21
Erin F. Munoz ----> 21
Troy G. Harrell ----> 23
Yen W. Summers ----> 21
Jerome B. Carney ----> 24
Yuri O. Head ----> 17
Ima A. Richard ----> 22
Eleanor Q. Guerrero ----> 21
Stuart F. Daniels ----> 15
Cara G. Beach ----> 19
Nolan E. Cortez ----> 23
Darius H. Beach ----> 14
Christian T. Mercer ----> 19
Florence F. Gordon ----> 25
Wilma I. Patton ----> 17
Rajah I. Jensen ----> 21
Zoe U. Whitfield ----> 19
May K. Wood ----> 13
Beck Q. Monroe ----> 24
Amanda A. Marks ----> 11
Serina X. Lang ----> 14
Lionel L. Bartlett ----> 17
Florence Z. Oconnor ----> 24
Chloe Y. Sears ----> 19
Mariam N. Tyler ----> 23
Dana A. Jefferson ----> 20
Nola N. Chan ----> 10
Owen Z. Odom ----> 20
Serina B. Nash ----> 18
Salvador I. Powell ----> 19
[6]:
#
# Retorno del registro como un diccionario
# =============================================================================
#
from collections import namedtuple

fieldnames = namedtuple(
    "Record",
    "Name, Address, City, ZipCode, Date, Score",
)

with open("/tmp/data.csv", "r") as csv_file:

    csv_reader = csv.reader(
        csv_file,
        delimiter=",",
        quotechar='"',
    )

    for row in map(fieldnames._make, csv_reader):
        print(row._asdict())
OrderedDict([('Name', 'Name'), ('Address', 'Address'), ('City', 'City'), ('ZipCode', 'ZipCode'), ('Date', 'Date'), ('Score', 'Score')])
OrderedDict([('Name', 'Isabelle I. Dotson'), ('Address', 'P.O. Box 477, 7357 Cras St.'), ('City', 'LamontzŽe'), ('ZipCode', '12144-86218'), ('Date', '02.24.21'), ('Score', '21')])
OrderedDict([('Name', 'Erin F. Munoz'), ('Address', 'P.O. Box 737, 6015 Ligula St.'), ('City', 'Springfield'), ('ZipCode', '6373'), ('Date', '10.15.20'), ('Score', '21')])
OrderedDict([('Name', 'Troy G. Harrell'), ('Address', '3706 Ut St.'), ('City', 'Herne'), ('ZipCode', '69259'), ('Date', '06.28.21'), ('Score', '23')])
OrderedDict([('Name', 'Yen W. Summers'), ('Address', 'P.O. Box 939, 8016 Egestas Av.'), ('City', 'Elversele'), ('ZipCode', '93452'), ('Date', '05.24.21'), ('Score', '21')])
OrderedDict([('Name', 'Jerome B. Carney'), ('Address', '298-7004 Natoque St.'), ('City', 'Osimo'), ('ZipCode', '20619'), ('Date', '09.23.20'), ('Score', '24')])
OrderedDict([('Name', 'Yuri O. Head'), ('Address', 'P.O. Box 214, 231 Dapibus Ave'), ('City', 'Lac-Serent'), ('ZipCode', '4464'), ('Date', '12.22.21'), ('Score', '17')])
OrderedDict([('Name', 'Ima A. Richard'), ('Address', 'Ap #496-7181 Ullamcorper, St.'), ('City', 'Lehrte'), ('ZipCode', '21449'), ('Date', '09.12.21'), ('Score', '22')])
OrderedDict([('Name', 'Eleanor Q. Guerrero'), ('Address', '5425 Ornare St.'), ('City', 'Arendonk'), ('ZipCode', '00636'), ('Date', '02.15.22'), ('Score', '21')])
OrderedDict([('Name', 'Stuart F. Daniels'), ('Address', '719-8478 Nunc St.'), ('City', 'Mataram'), ('ZipCode', '43168'), ('Date', '06.20.21'), ('Score', '15')])
OrderedDict([('Name', 'Cara G. Beach'), ('Address', '648-517 Velit. Av.'), ('City', 'Casciana Terme'), ('ZipCode', '78450'), ('Date', '02.12.22'), ('Score', '19')])
OrderedDict([('Name', 'Nolan E. Cortez'), ('Address', 'Ap #766-6998 Gravida Rd.'), ('City', 'Manfredonia'), ('ZipCode', '09534'), ('Date', '12.14.20'), ('Score', '23')])
OrderedDict([('Name', 'Darius H. Beach'), ('Address', 'Ap #251-7151 Donec Av.'), ('City', 'Valuyki'), ('ZipCode', '903927'), ('Date', '10.31.21'), ('Score', '14')])
OrderedDict([('Name', 'Christian T. Mercer'), ('Address', 'P.O. Box 858, 1997 Elit, Avenue'), ('City', 'Guaymas'), ('ZipCode', '21574'), ('Date', '10.20.20'), ('Score', '19')])
OrderedDict([('Name', 'Florence F. Gordon'), ('Address', '543-831 Est. Rd.'), ('City', 'Valcourt'), ('ZipCode', '6387'), ('Date', '12.01.21'), ('Score', '25')])
OrderedDict([('Name', 'Wilma I. Patton'), ('Address', 'P.O. Box 299, 7144 Orci Street'), ('City', 'Palermo'), ('ZipCode', '233311'), ('Date', '12.27.20'), ('Score', '17')])
OrderedDict([('Name', 'Rajah I. Jensen'), ('Address', 'Ap #782-6146 Turpis St.'), ('City', 'Arquata del Tronto'), ('ZipCode', '15936'), ('Date', '02.06.21'), ('Score', '21')])
OrderedDict([('Name', 'Zoe U. Whitfield'), ('Address', 'Ap #200-7243 Sit St.'), ('City', 'Chesapeake'), ('ZipCode', 'W2A 0QO'), ('Date', '12.01.21'), ('Score', '19')])
OrderedDict([('Name', 'May K. Wood'), ('Address', '777-1067 Auctor, Ave'), ('City', 'Maastricht'), ('ZipCode', '754025'), ('Date', '01.06.21'), ('Score', '13')])
OrderedDict([('Name', 'Beck Q. Monroe'), ('Address', 'P.O. Box 123, 9085 Lorem, Rd.'), ('City', 'Reading'), ('ZipCode', '27665'), ('Date', '08.03.22'), ('Score', '24')])
OrderedDict([('Name', 'Amanda A. Marks'), ('Address', '638-3732 Fermentum Road'), ('City', 'Sindelfingen'), ('ZipCode', '678968'), ('Date', '11.20.20'), ('Score', '11')])
OrderedDict([('Name', 'Serina X. Lang'), ('Address', 'P.O. Box 780, 2133 Rutrum Road'), ('City', 'Bromyard'), ('ZipCode', 'Z7366'), ('Date', '07.02.21'), ('Score', '14')])
OrderedDict([('Name', 'Lionel L. Bartlett'), ('Address', 'Ap #400-1963 Pellentesque. Ave'), ('City', 'Aparecida de Goiânia'), ('ZipCode', '744476'), ('Date', '12.21.21'), ('Score', '17')])
OrderedDict([('Name', 'Florence Z. Oconnor'), ('Address', '839 Cras Road'), ('City', 'Etawah'), ('ZipCode', '15197'), ('Date', '11.29.20'), ('Score', '24')])
OrderedDict([('Name', 'Chloe Y. Sears'), ('Address', '361-5600 Per St.'), ('City', 'Mission'), ('ZipCode', '0787 KM'), ('Date', '08.08.22'), ('Score', '19')])
OrderedDict([('Name', 'Mariam N. Tyler'), ('Address', '589-6397 Scelerisque Road'), ('City', 'Pozzuolo del Friuli'), ('ZipCode', 'T5G 6M8'), ('Date', '05.12.21'), ('Score', '23')])
OrderedDict([('Name', 'Dana A. Jefferson'), ('Address', 'Ap #142-8392 Consequat Avenue'), ('City', 'Khammam'), ('ZipCode', '49399'), ('Date', '02.06.21'), ('Score', '20')])
OrderedDict([('Name', 'Nola N. Chan'), ('Address', 'Ap #878-314 Faucibus Rd.'), ('City', 'Requínoa'), ('ZipCode', '1640'), ('Date', '06.10.22'), ('Score', '10')])
OrderedDict([('Name', 'Owen Z. Odom'), ('Address', '1659 Risus. Street'), ('City', 'Valuyki'), ('ZipCode', '12758'), ('Date', '02.03.21'), ('Score', '20')])
OrderedDict([('Name', 'Serina B. Nash'), ('Address', 'P.O. Box 143, 5197 At Avenue'), ('City', 'San Costantino Calabro'), ('ZipCode', '77234'), ('Date', '10.31.21'), ('Score', '18')])
OrderedDict([('Name', 'Salvador I. Powell'), ('Address', '356-7423 Semper Road'), ('City', 'Cardiff'), ('ZipCode', 'Z4197'), ('Date', '05.28.22'), ('Score', '19')])