測試時使用三種文件格式:sql
ISO-8859測試
Netpbm PBM image編碼
ASCIIspa
if [ $(file $filename|grep -c "ISO-8859") -gt 0 ]
then
echo "ISO-8859" psql -c "copy $schemaname.$tbname from '$dirname/$filename' with(format 'csv', delimiter ', encoding 'ISO-8859-1')" $dbname elif [ $(file $filename|grep -c "ASCII") -gt 0 ]
then
echo $format psql -c "copy $schemaname.$tbname from '$dirname/$filename' with(format 'csv', delimiter ', encoding 'UTF-8')" $dbname else psql -c "copy $schemaname.$tbname from '$dirname/$filename' with(format 'csv', delimiter ', encoding 'ISO-8859-1')" $dbname
這種處理方式在文件格式爲ASCII時,copy中途仍然會出現編碼錯誤提示。code
ERROR: invalid byte sequence for encoding "UTF8": 0xb3 orm
最後摸索發現無論什麼格式,都指定爲ISO-8859-1就能處理。blog
全包容的編碼格式,都能處理:it
psql -c "copy $schemaname.$tbname from '$dirname/$filename' with(format 'csv', delimiter ', encoding 'ISO-8859-1')" $dbnameform