简体   繁体   English

Django将csv文件复制到PostgreSQL表

[英]Django upload csv file copy to postgresql table

I am uploading a csv file in Django : 我正在Django中上传一个csv文件:

 $('#import-file-dialog').on('change', function(event) { $('#upload-files').click(); }); 
 <div class="" style="padding-left: 15px;"> <button class="btn btn-primary btn-outline" id="open-file-dialog">Upload Bulk</button> <input type="file" id="import-file-dialog" name="import_file_dialog" style="display: none;" /> <button class="btn btn-primary" id="upload-files" name="upload_import" style="display: none;">Upload Bulk</button> </div> 

Now i want to use psycopg2 copy_from for inserting csv data to Posgresql. 现在我想使用psycopg2 copy_from将csv数据插入Posgresql。 In django model: 在Django模型中:

fa = StringIO(import_data.read().decode("utf-8"))
cursor.copy_from(fa , table_name, sep=',')
commit_changes()

csv file data is getting store with csv header name in my table. csv文件数据正以csv标头名称存储在我的表中。 How i remove csv header so that i could store that data to table. 我如何删除csv标头,以便可以将数据存储到表中。

Thanks for your precious time. 感谢您的宝贵时间。

You are doing double-processing of the CSV file. 您正在对CSV文件进行双重处理。 csv.reader reads the file line-by-line and parses it into lists of fields. csv.reader读取文件并将其解析为字段列表。 copy_from reads a file or other object and parses into fields (default to tab, but with sep=',' it is effectively CSV). copy_from读取文件或其他对象并解析为字段(默认为tab,但使用sep=','实际上是CSV)。 You can either: 您可以:

a) read the file directly (ie, just open it normally instead of using csv ) and then use copy_from or b) use csv.reader to parse and then build queries to insert each row. a)直接读取文件(即,只需正常打开而不是使用csv ),然后使用copy_from或b)使用csv.reader进行解析,然后构建查询以插入每一行。

The first option is much simpler. 第一种选择要简单得多。 But keep in mind (from the cursor class docs ) that there is an optional columns parameter. 但是请记住(从游标类docs ),有一个可选的columns参数。 If you don't use it then The length and types should match the content of the file to read. 如果不使用它,则长度和类型应与要读取的文件内容匹配。 If not specified, it is assumed that the entire table matches the file structure. 如果未指定,则假定整个表与文件结构匹配。

Unfortunately, there is no "skip the header line" option for copy_from . 不幸的是, copy_from没有“跳过标题行”选项。 If you need to do that, you can either (if it is a one-time load) delete the line manually, or you can use copy_export - something like: 如果需要这样做,可以手动删除(如果是一次加载),也可以使用copy_export类似:

cursor.copy_expert(sql="COPY %s FROM STDIN WITH CSV HEADER DELIMITER AS ','" % table_name, file=fa)

Full example at Load a CSV File with Header in Postgres via Psycopg 通过Psycopg在Postgres中使用标题加载CSV文件的完整示例

For skipping your CSV file header and copy the data respectively, the code is pretty simple: 要跳过CSV文件标题并分别复制数据,代码非常简单:

cursor = conn.cursor()

with open(csv_file, 'r') as fp:
    next(f)  # Skip header
    cursor.copy_from(fp, table_name, sep=',', columns=columns)

PS Don't forget to pass a tale name and an ordered set of columns, that should exactly being matched to CSV file columns ordering PS不要忘记传递一个故事名称和一组有序的列,这些列应与CSV文件列的排序完全匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM