I have a csv file. I want to iterate the rows and produce sql string. I tried the solutions in stackoverflow but could not manage to fix it.
csv file
rating,product_type,upc,title
Three,Books,a897fe39b1053632,A Light in the Attic
One,Books,6957f44c3847a760,Soumission
python file starts with the following code
path = r'C:\Users\HP\PycharmProjects\book_crawler\books\items.csv'
file = open(path, 'rt')
I tried different version for string formatting. Some of the errors I get:
IndexError: tuple index out of range
for row in file:
print ('INSERT IGNORE INTO books_table(rating, product_type, upc, title) VALUES({},{},{},{})'.format(row))
TypeError: not all arguments converted during string formatting
for row in file:
print ('INSERT IGNORE INTO books_table(rating, product_type, upc, title) VALUES({0},{1},{2},{3})' % row)
TypeError: not all arguments converted during string formatting
for row in file:
print ('INSERT IGNORE INTO books_table(rating, product_type, upc, title) VALUES({0},{1},{2},{3})' % (row,))
TypeError: not all arguments converted during string formatting
for row in file:
print ('INSERT IGNORE INTO books_table(rating, product_type, upc, title) VALUES({0},{1},{2},{3})' % tuple(row))
insert? Just useload data infile.print(row). That will explain to us (and you) where the number of arguments is going wrong.next()to skip it, also make surerowisn't empty before processing it. Divide your code into blocks and debug it properly.