简体   繁体   中英

Python - Remove special character from csv file import into PostgreSQL

I would like to import my csv file into Postgresql using Python. The import works well. However, when I display the imported data, I find a special symbol on the first line and first column. I tried to solve the problem by adding the encoding in my python code but nothing has to do. Here is my code:

import sys
import os
import csv
import io
f = io.open(r'C:\\list.csv', mode='r', encoding='utf-8')
curs.copy_from(f, 'list', sep=';')
conn.commit()

Here is the symbol or special character:



在此处输入图片说明

Thank you

You are picking up the Byte order mark .

In order to have the io module expect and strip off the BOM try changing your encoding to utf-8-sig :

f = io.open(r'C:\\list.csv', mode='r', encoding='utf-8-sig')

More info here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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