亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

寫入 CSV:“UnicodeDecodeError:‘charmap’編解碼器無法解碼字節

寫入 CSV:“UnicodeDecodeError:‘charmap’編解碼器無法解碼字節

肥皂起泡泡 2022-11-18 20:55:14
我試圖將一個大的 csv 文件拆分成多個文件,為此我使用了這個代碼片段。我正在使用 Python 3.7.7 并且在 Windows 操作系統上。我嘗試添加 utf8 編碼,但它仍然不起作用。你知道為什么嗎?這是我的代碼:import osdef split(filehandler, delimiter=',', row_limit=125000, output_name_template='jokes_%s.csv', output_path='.', keep_headers=True):    """    Splits a CSV file into multiple pieces.    A quick bastardization of the Python CSV library.    Arguments:        `row_limit`: The number of rows you want in each output file. 10,000 by default.        `output_name_template`: A %s-style template for the numbered output files.        `output_path`: Where to stick the output files.        `keep_headers`: Whether or not to print the headers in each output file.    Example usage:        >> from toolbox import csv_splitter;        >> csv_splitter.split(open('/home/ben/input.csv', 'r'));    """    import csv    reader = csv.reader(filehandler,  delimiter=delimiter)    current_piece = 1    current_out_path = os.path.join(         output_path,         output_name_template  % current_piece    )    print(current_out_path)    current_out_writer = csv.writer(open(current_out_path, 'w', encoding='utf8', newline=''), delimiter=delimiter)    current_limit = row_limit    if keep_headers:        headers = next(reader)        current_out_writer.writerow(headers)    for i, row in enumerate(reader):        if i + 1 > current_limit:            current_piece += 1            current_limit = row_limit * current_piece            current_out_path = os.path.join(               output_path,               output_name_template  % current_piece            )            print(current_out_path)            current_out_writer = csv.writer(open(current_out_path, 'w', encoding='utf8', newline=''), delimiter=delimiter)            if keep_headers:                current_out_writer.writerow(headers)        current_out_writer.writerow(row)split(open('jokes.csv', 'r'))
查看完整描述

1 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

split(open('jokes.csv', 'r'))你可以換個split(open('jokes.csv', 'r', encoding="utf8"))試試。



查看完整回答
反對 回復 2022-11-18
  • 1 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號