SQLite3编码拉丁1 d

2024-05-14 03:48:49 发布

您现在位置:Python中文网/ 问答频道 /正文

例如,我有一个CSV文件,其中包含许多行,其中的数据是拉丁语-1

 Àird a' Bhàigh

我试图使用DictReader将这些数据加载到一个SQLite中,我尝试过的每个选项都无法在SQLite中创建正确的数据。在

选项1:Unicode

^{pr2}$

选项2:解码

reader = csv.DictReader(open('data.txt', "rb"), delimiter=':')
for row in reader:
     to_db = [row[0].decode('latin-1')]
     cur.execute("INSERT INTO table (name) VALUES (?);", to_db)

选项3:解码和编码

reader = csv.DictReader(open('data.txt', "rb"), delimiter=':')
for row in reader:
     to_db = [row[0].decode('latin-1').encode('utf-8')]
     cur.execute("INSERT INTO table (name) VALUES (?);", to_db)

对于每个选项,我都会遇到各种错误,包括

'utf8' codec can't decode byte 0xe0 in position 4

You must not use 8-bit bytestrings

所以我把连接改成:

connection.text_factory = str

我再一次在数据中发现了黑色的问号。在

这非常令人沮丧,因为用PostgreSQL这样的代码设置非常简单

希望有人能帮忙解决问题

谢谢

更新时间:

这是文件的几行

2467:NB2601:Àird a' Bhàigh:NB20:57:55.1:6:37.2:901500:126500:W:WI:N Eil:Na h-Eileanan an Iar:H:01-MAR-1993:I:14:0:0
2468:NB2034:Àird a' Chaolais:NB22:58:12.6:6:45.5:934500:120500:W:WI:N Eil:Na h-Eileanan an Iar:X:01-MAR-1993:I:8:13:0
2469:NL6197:Àird a' Chaolais:NL68:56:56.7:7:34.1:797500:61500:W:WI:N Eil:Na h-Eileanan an Iar:X:01-MAR-1993:I:31:0:0
2470:NB4232:Àird a' Chleirich:NB42:58:12.4:6:23:932500:142500:W:WI:N Eil:Na h-Eileanan an Iar:X:01-MAR-1993:I:8:0:0
2471:NM4118:Àird a' Chrainn:NM40:56:17.3:6:10.6:718500:141500:W:AR:Arg & Bt:Argyll and Bute:X:01-MAR-1993:I:48:0:0
2472:NM7331:Àird a' Chrotha:NM62:56:25.3:5:40.4:731500:173500:W:AR:Arg & Bt:Argyll and Bute:X:01-JUL-1996:U:49:0:0
2473:NB2045:Àird a' Ghobhann:NB24:58:18.5:6:46.3:945500:120500:W:WI:N Eil:Na h-Eileanan anIar:X:01-JAN-1995:U:8:0:0
2474:NF7445:Àird a' Mhachair:NF64:57:23:7:25.1:845500:74500:W:WI:N Eil:Na h-Eileanan an Iar:O:05-JAN-2012:U:22:0:0
2475:NF8379:Àird a' Mhòrain:NF86:57:41.6:7:18.8:879500:83500:W:WI:N Eil:Na h-Eileanan an Iar:H:01-MAR-1993:I:18:0:0
2476:NB1810:Àird a' Mhulaidh:NB00:57:59.7:6:45.9:910500:118500:W:WI:N Eil:Na h-Eileanan an Iar:O:01-MAY-1997:U:13:14:0
2477:NF7974:Àird a' Phuind:NF66:57:38.8:7:22.4:874500:79500:W:WI:N Eil:Na h-Eileanan an Iar:X:01-MAR-1993:I:18:0:0

你可以看到这是我正在挣扎的第三个专栏。在

这就是通过Firefox使用浏览器在SQLite中的样子

enter image description here

更新2:当我打印repr(行[0])时,我得到以下内容

"\xc0ird a' Bh\xe0igh"
"\xc0ird a' Chaolais"
"\xc0ird a' Chaolais"
"\xc0ird a' Chleirich"
"\xc0ird a' Chrainn"
"\xc0ird a' Chrotha"
"\xc0ird a' Ghobhann"
"\xc0ird a' Mhachair"
"\xc0ird a' Mh\xf2rain"
"\xc0ird a' Mhulaidh"
"\xc0ird a' Phuind"

我怎样才能确保它是拉丁语,这就是我相信那些字符是其中一部分的字符集


Tags: to数据andb选项marreaderrow