Python 字符串格式:迭代csv文件中的行值

Python 字符串格式:迭代csv文件中的行值,python,mysql,sql,string,python-3.x,Python,Mysql,Sql,String,Python 3.x,我有一个csv文件。我想迭代这些行并生成sql字符串。我尝试了stackoverflow中的解决方案,但无法修复它 csv文件 rating,product_type,upc,title Three,Books,a897fe39b1053632,A Light in the Attic One,Books,6957f44c3847a760,Soumission python文件以以下代码开头 path = r'C:\Users\HP\PycharmProjects\book_crawler

我有一个csv文件。我想迭代这些行并生成sql字符串。我尝试了stackoverflow中的解决方案,但无法修复它

csv文件

rating,product_type,upc,title

Three,Books,a897fe39b1053632,A Light in the Attic

One,Books,6957f44c3847a760,Soumission
python文件以以下代码开头

path = r'C:\Users\HP\PycharmProjects\book_crawler\books\items.csv'
file = open(path, 'rt')
我尝试了不同版本的字符串格式。我得到的一些错误:

索引器错误:元组索引超出范围

rating,product_type,upc,title

Three,Books,a897fe39b1053632,A Light in the Attic

One,Books,6957f44c3847a760,Soumission
TypeError:在字符串格式化过程中,并非所有参数都已转换

TypeError:在字符串格式化过程中,并非所有参数都已转换

TypeError:在字符串格式化过程中,并非所有参数都已转换


我不完全确定您想做什么,但要解析csv文件并使用csv值生成
mysql
查询,您可以使用:

import csv
csv_path = "C:/Users/HP/PycharmProjects/book_crawler/books/items.csv"
with open(csv_path) as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    # skip the first line
    next(readCSV) 
    for row in readCSV:
        # skip blank lines
        if row: 
            # assign variables
            rating = row[0]; product_type = row[1]; upc = row[2]; title = row[3]
            # surround table and fields with  back-tick ` and values with single quote '
            print ("INSERT IGNORE INTO `books_table` (`rating`, `product_type`, `upc`, `title`) VALUES('{}', '{}', '{}', '{}')".format(rating, product_type, upc, title))

输出:

INSERT IGNORE INTO `books_table` (`rating`, `product_type`, `upc`, `title`) VALUES('Three', 'Books', 'a897fe39b1053632', 'A Light in the Attic')
INSERT IGNORE INTO `books_table` (`rating`, `product_type`, `upc`, `title`) VALUES('One', 'Books', '6957f44c3847a760', 'Soumission')

为什么要创建一个
插入
?只需使用
加载数据填充
。首先执行
打印(行)
。这将向我们(和您)解释参数的数量哪里出了问题。您确实解析了CSV文件吗?您可能正在解析CSV的第一行,请使用
next()
跳过它,并确保
行在处理它之前不为空。将代码分成块并正确调试。这是我代码的一部分。我正在抓取一些数据来加载mysql。
for row in file:
    print ('INSERT IGNORE INTO books_table(rating, product_type, upc, title) VALUES({0},{1},{2},{3})' % tuple(row))
import csv
csv_path = "C:/Users/HP/PycharmProjects/book_crawler/books/items.csv"
with open(csv_path) as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    # skip the first line
    next(readCSV) 
    for row in readCSV:
        # skip blank lines
        if row: 
            # assign variables
            rating = row[0]; product_type = row[1]; upc = row[2]; title = row[3]
            # surround table and fields with  back-tick ` and values with single quote '
            print ("INSERT IGNORE INTO `books_table` (`rating`, `product_type`, `upc`, `title`) VALUES('{}', '{}', '{}', '{}')".format(rating, product_type, upc, title))
INSERT IGNORE INTO `books_table` (`rating`, `product_type`, `upc`, `title`) VALUES('Three', 'Books', 'a897fe39b1053632', 'A Light in the Attic')
INSERT IGNORE INTO `books_table` (`rating`, `product_type`, `upc`, `title`) VALUES('One', 'Books', '6957f44c3847a760', 'Soumission')