第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

在python中導(dǎo)出utf-8 csv文件

在python中導(dǎo)出utf-8 csv文件

Qyouu 2021-03-29 17:04:08
在python中導(dǎo)出utf-8 csv文件時(shí)遇到錯(cuò)誤。錯(cuò)誤說AttributeError: 'int' object has no attribute 'encode'首先,我使用pyodbc連接Microsoft Access數(shù)據(jù)庫(kù)并在那里獲取數(shù)據(jù)。MDB = "E:/Research/2000-01.mdb"; DRV = '{Microsoft Access Driver (*.mdb)}'; PWD = 'pw'con = pyodbc.connect('DRIVER={};DBQ={};PWD={}'.format(DRV,MDB,PWD))cur = con.cursor()SQL = 'SELECT * FROM 200001;'rows = cur.execute(SQL).fetchall()cur.close()con.close()然后使用該類,class UnicodeWriter:    """    A CSV writer which will write rows to CSV file "f",    which is encoded in the given encoding.    """def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):    # Redirect output to a queue    self.queue = cStringIO.StringIO()    self.writer = csv.writer(self.queue, dialect=dialect, **kwds)    self.stream = f    self.encoder = codecs.getincrementalencoder(encoding)()def writerow(self, row):    self.writer.writerow([s.encode("utf-8") for s in row])    # Fetch UTF-8 output from the queue ...    data = self.queue.getvalue()    data = data.decode("utf-8")    # ... and reencode it into the target encoding    data = self.encoder.encode(data)    # write to the target stream    self.stream.write(data)    # empty queue    self.queue.truncate(0)def writerows(self, rows):    for row in rows:        self.writerow(row)我開始寫utf-8 csv文件with open("E:/Research/200001.txt", 'wb') as f:    writer = UnicodeWriter(f)    writer.writerows(rows)看起來(lái)每一行都包含一些整數(shù)和日期時(shí)間的東西。有解決這個(gè)問題的想法嗎?非常感謝!
查看完整描述

1 回答

?
慕哥6287543

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超10個(gè)贊

您可能需要先執(zhí)行以下操作,然后再執(zhí)行以下操作writer.writerows

rows = [[unicode(x) for x in row] for row in rows]

或者,我的猜測(cè)是試圖寫數(shù)據(jù)庫(kù)行ID的麻煩。因此,您也可以嘗試將其切成薄片:

rows = [row[1:] for row in rows]


查看完整回答
反對(duì) 回復(fù) 2021-04-02
  • 1 回答
  • 0 關(guān)注
  • 359 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)