这是tx.sql
DECLARE @Cnt INT,
@ParticipantID UNIQUEIDENTIFIER
SELECT ParticipantID INTO #ids
FROM dbo.rbd_Participants
/* sun */
WHERE surname='Пупкин'
这是python脚本
with open('tx.sql', 'r') as f:
script = f.read().decode('utf8')
script = re.sub(r'\/\*.*?\*\/', '', script, flags=re.DOTALL)multiline comment
script = re.sub(r'--.*$', '', script, flags=re.MULTILINE) line comment
sql = []
do_execute = False
for line in script.split(u'\n'):
line = line.strip()
if not line:
continue
elif line.upper() == u'GO':
do_execute = True
else:
sql.append(line)
do_execute = line.endswith(u';')
#print line
cur.execute(u'\n'.join(sql).encode('utf8'))
问题专栏:script = f.read().decode('utf8')
UnicodeDecodeError:'utf8'编解码器无法将字节0xc0解码到位 134:无效的起始字节
我试过了
script = f.read().decode('cp1251')
但行
cur.execute(u'\n'.join(sql).encode('utf8'))
print (u'\n'.join(sql)).encode('utf8')
DECLARE @Cnt INT,
@ParticipantID UNIQUEIDENTIFIER
SELECT ParticipantID INTO #ids
FROM dbo.rbd_Participants
WHERE surname='РџСѓРїРєРёРЅ'
如何制作正确的广告?
WHERE surname ='РџСѓРїРєРёРЅ'
必须有一个字符串
WHERE surname ='Пупкин'
答案 0 :(得分:2)
您正在正确阅读数据。您的打印语句不正确:
print (u'\n'.join(sql)).encode('utf8')
您的终端或控制台不支持UTF-8,因此它向您显示错误的数据。不要编码,把它留给Python。