所以,我导入了一个CSV文件,它可以包含任意数量的列和行。我想将结果存储在临时表中,例如:“TEMP_30f3724fc226e058”,而不是在多个页面上处理CSV文件。
我已经得到了以下内容,但除此之外,我不确定如何添加本质上是未知数量的列和行。任何想法都会很棒。
key = '30f3724fc226e058'
cursor = connection.cursor()
db_name = 'TEMP_{0}'.format(key)
cursor.execute('CREATE TEMPORARY TABLE {0} (X INT)'.format(db_name))
... ?
从CSV插入数据,然后我可以在CSV导入过程中的任何位置提取该数据。再次感谢,
答案 0 :(得分:1)
步骤是:
这样的事情:
import csv
import time
column_count = 2 # Assume we have 2 columns, which is the minimum
with open('somefile.txt') as f:
reader = csv.reader(f, delimiter=',')
# fetch the first row, grab the column length
column_count = len(next(reader))
# Next, create the table:
table_name = 'sometable_{0}'.format(int(time.time()))
q = 'CREATE TEMPORARY TABLE {0} ('.format(table_name)
q += ', '.join('col_{0} VARCHAR(255)'.format(i) for i in range(column_count))
q += ');'
cur.execute(q)
cur.commit()
q = "INSERT INTO {0} VALUES (".format(table_name)
q += ', '.join(('%s ' * column_count).split())
q += ');'
# Now, populate it
with open('somefile.txt') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
cur.execute(q, tuple(row))
cur.commit()