简体   繁体   English

python Postgresql:忽略csv文件中的最后一列

[英]python Postgresql: Ignoring the last column from csv file

I have problem with importing a CSV file. 我在导入CSV文件时遇到问题。 I am using postgresql's COPY FROM command to copy a CSV file into a 2-column table. 我正在使用postgresql的COPY FROM命令将CSV文件复制到2列表中。

I have a CSV file in the following format; 我有以下格式的CSV文件;

"1";"A"
"2";"B"
"3";"C";"CAD450"
"4";"D";"ABX123"

I want to import all these lines of the CSV file into the table but I want to skip any extra added columns. 我想将CSV文件的所有这些行导入到表中,但是我想跳过任何额外添加的列。

Currently I am skipping any lines that contain extra columns, for example here columns "1";"C";"CAD450" and "1";"D";"ABX123" are skipped and I am importing only the first two columns. 当前,我正在跳过包含额外列的任何行,例如,这里的列"1";"C";"CAD450""1";"D";"ABX123"被跳过,并且仅导入前两列。 But I want to copy all these four lines into my table. 但是我想将所有这四行复制到我的表中。 So is there any way where I can ignore the last column and copy all the four lines into my table, like this 所以有什么办法可以忽略最后一列并将所有四行复制到我的表中,就像这样

"1";"A"
"1";"B"
"1";"C"
"1";"D"

用awk预处理文件以去除多余的列:

awk -F';' '{print $1 ";" $2 }' > new_file.csv

通过cutawk配管(如上所述)比使用python / psycopg容易。

cat csv_file.csv | cut -d';' -f1,2 | psql -u USER DATABASE -c "COPY table FROM STDIN WITH DELIMITER ';';"

with open("file.csv","r") as f:
    t=[line.strip().split(";")[:2] for line in f]

Myriad ways to handle the problem. 处理问题的方法多种多样。
I'd probably do something like this: 我可能会做这样的事情:

import csv
import psycopg2
dr = csv.DictReader(open('test.csv','rb'), 
                    delimiter=';',
                    quotechar='"',
                    fieldnames=['col1','col2']) # need not specify other cols
CONNSTR = """
  host=127.0.0.1 
  dbname=mydb
  user=me
  password=pw
  port=5432"""
cxn = psycopg2.connect(CONNSTR)
cur = cxn.cursor()
cur.execute("""CREATE TABLE from_csv (
               id serial NOT NULL,
               col1 character varying,
               col2 character varying,
               CONSTRAINT from_csv_pkey PRIMARY KEY (id));""")
cur.executemany("""INSERT INTO from_csv (col1,col2) 
                   VALUES (%(col1)s,%(col2)s);""", dr)
cxn.commit()

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

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