用于编码的无效字节序列“UTF8” [英] invalid byte sequence for encoding "UTF8"

查看:330
本文介绍了用于编码的无效字节序列“UTF8”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试导入一些数据进入我的数据库。所以我创建了一个临时表,

I'm trying to import some data into my database. So I've created a temporary table,

create temporary table tmp(pc varchar(10), lat decimal(18,12), lon decimal(18,12), city varchar(100), prov varchar(2));

现在我正在尝试导入数据

 copy tmp from '/home/mark/Desktop/Canada.csv' delimiter ',' csv

然后我收到错误,

ERROR:  invalid byte sequence for encoding "UTF8": 0xc92c

我如何解决这个问题?我是否需要更改整个数据库的编码(如果是,如何?)或者我是否可以仅更改 tmp 表的编码?或者我应该尝试更改文件的编码?

How do I fix that? Do I need to change the encoding of my entire database (if so, how?) or can I change just the encoding of my tmp table? Or should I attempt to change the encoding of the file?

推荐答案

如果需要在数据库中存储UTF8数据,则需要一个接受UTF8的数据库。您可以在pgAdmin中检查数据库的编码。只需右键单击数据库,然后选择属性。

If you need to store UTF8 data in your database, you need a database that accepts UTF8. You can check the encoding of your database in pgAdmin. Just right-click the database, and select "Properties".

但是该错误似乎告诉您源文件中存在一些无效的UTF8数据。这意味着 copy 实用程序检测到或猜到你正在为它提供一个UTF8文件。

But that error seems to be telling you there's some invalid UTF8 data in your source file. That means that the copy utility has detected or guessed that you're feeding it a UTF8 file.

如果你'在Unix的某些变种下运行,您可以使用 <$检查编码(或多或少) c $ c> file utility。

If you're running under some variant of Unix, you can check the encoding (more or less) with the file utility.

$ file yourfilename
yourfilename: UTF-8 Unicode English text

(我认为这也适用于终端上的Mac。 )不确定如何在Windows下执行此操作。

(I think that will work on Macs in the terminal, too.) Not sure how to do that under Windows.

如果对来自Windows系统的文件使用相同的实用程序(即,不是的文件以UTF8编码),它可能会显示如下内容:

If you use that same utility on a file that came from Windows systems (that is, a file that's not encoded in UTF8), it will probably show something like this:

$ file yourfilename
yourfilename: ASCII text, with CRLF line terminators

如果事情变得奇怪,您可能会尝试将输入数据转换为一种已知的编码,用于更改客户端的编码,或两者兼而有之。 (我们真的在扩展我对编码知识的限制。)

If things stay weird, you might try to convert your input data to a known encoding, to change your client's encoding, or both. (We're really stretching the limits of my knowledge about encodings.)

你可以使用 iconv 实用程序,用于更改输入数据的编码。

You can use the iconv utility to change encoding of the input data.

iconv -f original_charset -t utf-8 originalfile > newfile

您可以按照字符集支持。在该页面上,搜索短语启用自动字符集转换。

You can change psql (the client) encoding following the instructions on Character Set Support. On that page, search for the phrase "To enable automatic character set conversion".

这篇关于用于编码的无效字节序列“UTF8”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆