2

I need to create a where only the file Base_autos.csv should be store in database created by PosgresSQL

In this program I should have:

name of the database: concesionario name of the table: autos

I should create a module(name of the module conexion_postgres.py) which is in charge of: Creating a connection enter the reading data from the file Base_autos.csv, Edit records(sending like parameters id) eliminating records, all errors should be controlled and inside of the Base_autos.csv has the following elements:

Empresa;Año;Monto Ventas;Mercado;Cantidad
Chevrolet;2010;105;20;95
Targit;2010;68;114;46
Ufan;2010;101;156;55
Volkswagen;2010;12;25;13
Renault;2010;11;253;143
Chevrolet;2010;100;20;3
Hynduai;2010;12;217;97
Porsche;2010;100;20;3
Skoda;2010;100;20;3
Hino;2010;65;116;51
Mazda;2010;149;274;125
Citroen;2010;112;24;128
Kia;2010;148;26;112
Mitsubishi;2010;100;20;3
Honda;2010;100;20;3
UB Trucks;2010;103;214;111
Toyota;2010;128;287;159
Mercedes;2010;81;136;55
Chery;2010;73;125;52
Nissan;2010;100;20;3
Jmc;2010;5;9;4
Chevrolet;2011;111;208;97
Targit;2011;89;148;59
Ufan;2011;112;182;7
Volkswagen;2011;121;232;111
Renault;2011;105;249;144
Chevrolet;2011;78;126;48
Hynduai;2011;117;232;115
Porsche;2011;100;20;3
Skoda;2011;100;20;3
Hino;2011;45;86;41
Mazda;2011;142;27;128
Citroen;2011;135;264;129
Kia;2011;151;28;129
Mitsubishi;2011;98;155;57
Honda;2011;54;108;54
UB Trucks;2011;127;229;102
Toyota;2011;129;289;16
DongFeng;2011;65;111;46
Mercedes;2011;92;179;87
BMW;2011;73;119;46
Chery;2011;7;119;49
Nissan;2011;100;20;3
Jmc;2011;59;10;41
Chevrolet;2012;138;234;96
Targit;2012;4;91;51
Ufan;2012;145;226;81
Volkswagen;2012;115;225;11
Renault;2012;104;246;142
Chevrolet;2012;66;107;41
Hynduai;2012;13;238;108
Porsche;2012;71;91;2
Skoda;2012;26;82;56
Hino;2012;76;136;6
Mazda;2012;149;27;121
Citroen;2012;146;271;125
Kia;2012;134;262;128
Mitsubishi;2012;95;156;61
Honda;2012;2;85;65
UB Trucks;2012;132;234;102
Toyota;2012;125;295;17
Mercedes;2012;61;136;75
Chery;2012;49;105;56
Nissan;2012;35;84;49
Jmc;2012;8;127;47
Chevrolet;2013;100;20;3
Chevrolet;2013;133;247;114
Targit;2013;47;89;42
Ufan;2013;153;256;103
Volkswagen;2013;141;274;133
Renault;2013;102;257;155
Chevrolet;2013;74;131;57
Hynduai;2013;151;266;115
Porsche;2013;100;20;3
Porsche;2013;96;146;5
Skoda;2013;61;115;54
Hino;2013;79;168;89
Mazda;2013;147;271;124
Citroen;2013;135;257;122
Kia;2013;16;293;133
Mitsubishi;2013;119;187;68
Honda;2013;66;115;49
UB Trucks;2013;132;234;102
Great Wall;2013;100;20;3
Toyota;2013;145;315;17
Ford;2013;45;107;62
DongFeng;2013;100;20;3
Mercedes;2013;8;135;55
BMW;2013;91;136;45
Lifan;2013;115;191;76
Chery;2013;4;88;48
Nissan;2013;7;166;96
Jmc;2013;82;161;79
Chevrolet;2014;82;133;51
Chevrolet;2014;116;238;122
Targit;2014;59;109;5
Ufan;2014;153;276;123
Volkswagen;2014;119;257;138
Renault;2014;118;241;123
Chevrolet;2014;78;124;46
Hynduai;2014;14;263;123
Porsche;2014;42;6;18
Porsche;2014;98;178;8
Skoda;2014;92;188;96
Hino;2014;87;19;103
Mazda;2014;109;23;121
Citroen;2014;106;223;117
Kia;2014;136;261;125
Mitsubishi;2014;11;161;51
Honda;2014;96;174;78
UB Trucks;2014;107;21;103
Great Wall;2014;32;67;35
Toyota;2014;119;259;14
Ford;2014;98;183;85
DongFeng;2014;100;20;3
Mercedes;2014;88;151;63
BMW;2014;91;147;56
Lifan;2014;12;218;98
Chery;2014;5;99;49
Nissan;2014;98;216;118
Jmc;2014;69;118;49
Chevrolet;2015;77;145;68
Chevrolet;2015;95;205;11
Targit;2015;8;143;63
Ufan;2015;146;257;111
Volkswagen;2015;108;253;145
Renault;2015;104;246;142
Chevrolet;2015;58;118;6
Hynduai;2015;121;228;107
Porsche;2015;87;15;63
Porsche;2015;92;182;9
Skoda;2015;78;176;98
Hino;2015;78;182;104
Mazda;2015;102;214;112
Citroen;2015;105;217;112
Kia;2015;112;219;107
Mitsubishi;2015;108;205;97
Honda;2015;100;20;3
UB Trucks;2015;104;21;106
Great Wall;2015;100;20;3
Toyota;2015;105;244;139
Ford;2015;91;171;8
Jacr;2015;74;165;91
DongFeng;2015;100;20;3
Mercedes;2015;74;127;53
BMW;2015;100;20;3
Lifan;2015;117;213;96
Chery;2015;100;20;3
Nissan;2015;86;193;107
Jmc;2015;44;112;68
Chevrolet;2016;32;94;62
Chevrolet;2016;43;147;104
Targit;2016;100;20;3
Ufan;2016;118;247;129
Chevrolet;2016;5;123;73
Volkswagen;2016;95;219;124
Renault;2016;86;211;125
Chevrolet;2016;100;20;3
Volvo;2016;55;119;64
Hynduai;2016;112;25;138
Porsche;2016;47;102;55
Porsche;2016;100;20;3
Porsche;2016;22;108;86
Skoda;2016;5;156;106
Hino;2016;100;20;3
Mazda;2016;100;20;3
Citroen;2016;77;194;117
Kia;2016;109;271;162
Mitsubishi;2016;54;1104;105
Honda;2016;100;20;3
UB Trucks;2016;5;125;75
Great Wall;2016;100;20;3
Toyota;2016;57;193;136
Ford;2016;72;149;77
QRC;2016;75;153;78
Jacr;2016;12;92;8
DongFeng;2016;100;20;3
Zotye;2016;52;153;101
Mercedes;2016;53;131;78
BMW;2016;100;20;3
Lifan;2016;95;188;93
Jac;2016;31;15;119
Chery;2016;100;20;3
Nissan;2016;97;226;129
Jmc;2016;100;20;3

AND MY SOLUTION WAS THIS

import csv
import psycopg2
conn = psycopg2.connect("host=localhost dbname=postgres user=postgres")
cur = conn.cursor()
with open('Base_autos.csv', 'r') as f:
    reader = csv.reader(f)
    next(reader) 
    for row in reader:
        cur.execute(
        "INSERT INTO Autos VALUES (%s, %s, %s, %s, %s)",
        row
    )
conn.commit()

but now it shows an error

Traceback (most recent call last):
  File "C:\Users\Hugo\Desktop\CYBER\PYTHON\djangoproyecto\tarea5.py", line 9, in <module>
    cur.execute(
IndexError: list index out of range

how do I fix this?

1
  • 1
    The specific question has already been answered; more generally, I would suggest a "how to debug" article like ericlippert.com/2014/03/05/how-to-debug-small-programs which will help you either solve the problem altogether, or at least narrow down the question to just the specific couple of lines to ask about here on SO Commented Jul 6, 2021 at 2:15

1 Answer 1

2

The delimiter you are using in the csv file is a semi-colon, but csv.reader defaults to a comma, so intead of setting row to a 5 element list of the 5 items in each row, it is being set to a list of 1 element, which contains a single string.

To fix this, just specify the delimiter in the call to csv, like this...

reader = csv.reader(f, delimiter=';')

The error message says the list index is out of range because row in your code only has 1 element, but %s, %s, %s, %s, %s requires a list of 5 elements to fill each of the 5 positions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.