本文目錄一覽:
- 1、python3.6 cx_oracle連接數據庫報編碼錯UnicodeDecodeError
- 2、python經典類和新式類為啥效率差這麼多
- 3、如何寫一段python代碼,提取並保存txt里相應格式的內容?
python3.6 cx_oracle連接數據庫報編碼錯UnicodeDecodeError
我說下我遇到的情況
數據庫字符集是 ZHS16GBK
錯誤的情況是
UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xa7 in position 12: illegal multibyte sequence
經過檢查,在fetchall()獲取記錄時,查詢到的記錄裏面有亂碼(應該是不包含在數據庫現有字符集下的字符)
臨時的一個解決辦法是
db=cx_Oracle.connect(dblink,encoding=’UTF-8′)
這樣可以讀取了,讀取到的內容為
廣州市\ue738同泰路
其中 ‘\ue738’應該是之前不可被讀取的字符,希望對各位有幫助
python經典類和新式類為啥效率差這麼多
你的測試時間差別太誇張了,肯定受到其他程序的影響,用timeit測量了一下更好。
如果all_except函數是 current != node這種,大概舊式類的對象創建和遍歷加的操作是新式類對象的時間2倍。但是如果把all_except改成 while not current is node:那麼舊式類對象的操作時間就比新式類少(新式類有一個descriptor的查找過程),如果給新式類增加__slots__,時間能稍微降一點,但還是比舊式類多。
所以問題應該是 != 操作沒有 not is 操作高效,is操作使用了id,應該類似比較內存地址那種。而!=可能做了更多的事情。
下面是代碼。
import time
class dblink:
def __init__(self,index=0):
self.index = index
self.prev = self
self.next = self
def insert(self,index):
node = dblink(index)
node.prev = self.prev
node.next = self
self.prev.next = node
self.prev = node
class nt_dblink(object):
__slots__=(‘index’,’prev’,’next’)
def __init__(self,index=0):
self.index = index
self.prev = self
self.next = self
def insert(self,index):
node = nt_dblink(index)
node.prev = self.prev
node.next = self
self.prev.next = node
self.prev = node
def all_except(node):
current = node.next
while not current is node:
yield current
current = current.next
def test1():
head = dblink()
for i in range(100000):
head.insert(i)
for node in all_except(head):
node.index+=1
def test2():
head = nt_dblink()
for i in range(100000):
head.insert(i)
for node in all_except(head):
node.index+=1
if __name__==’__main__’:
import timeit
print(timeit.timeit(“test1()”, setup=”from __main__ import test1″,number=1))
print(timeit.timeit(“test2()”, setup=”from __main__ import test2″,number=1))
如何寫一段python代碼,提取並保存txt里相應格式的內容?
import re
with open(input(“請輸入文件名:\n”), “r”) as f:
content = f.read()
with open(“current_txt.txt”, “w”) as f:
for t in re.findall(“Accession: /dtdd(.+?)/dd”, contents, re.S):
f.write(t)
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/293498.html