python-3.x - csv.DictReader 和 Session.bulk_save_objects?
问题描述
带有 SQLAlchemy 1.3 (MySQL 8) 的 Python 3.8
表定义:
在您询问之前,我无法提供 csv,因为它包含敏感数据。这是一个以 0x0A (\n) 作为换行符的“普通”csv 文件。在文本模式下打开时,DictReader
工作但bulk_save_objects
失败。
首先,我从 S3 下载文件:
def download_files(bucket, keys):
filenames = []
for key in keys:
filename = key.split('/')[-1]
filenames.append(filename)
with open(f'data/{filename}', mode='wb') as f:
bucket.download_fileobj(key, f)
return filenames
没有mode='wb'
:
TypeError: write() argument must be str, not bytes
然后,我解析文件并保存:
def parse_files(filenames):
objects = []
for filename in filenames:
with open(f'data/{filename}', mode='rb') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
row['movie_studios'] = row['movie_studios'].strip('"[]')
obj = Outbound(filename=filename, **row)
objects.append(obj)
session.bulk_save_objects(objects)
session.commit()
与mode='rb'
:
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
没有mode='rb'
和有[encoding='utf-8', ]newline=''
:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 30-33: character maps to <undefined>
没有mode='rb'
和有encoding='ascii', newline=''
:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 6658: ordinal not in range(128)
我试过 io.TextIOWrapper:
with open(f'data/{filename}', mode='rb') as f:
with io.TextIOWrapper(f, encoding='utf-8', newline='') as csvfile:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 30-33: character maps to <undefined>
不奇怪...
这个角色似乎失败了:
我该如何解决这个问题?
解决方案
我会检查 SQLAlchemy 的连接编码,以确保它使用utf-8
而不是asci
. 例如,指定charset
它在他们的示例中显示的内容:
e = create_engine(
"mysql+pymysql://scott:tiger@localhost/test?charset=utf8mb4")
推荐阅读
- r - 如何将多元丰度矩阵转换为R中的出现表?
- html - 位置固定在子元素中,但同步父 x 滚动与子元素左侧位置
- apache-spark - Spark Dataframe 默认是如何分区的?
- android - 如何从 ViewPager 片段中打开另一个片段?
- node.js - Gulp 项目在本地运行但无法构建
- asynchronous - 错误 TIMEOUT WAITING FOR ACK - RabbitMq Publisher 确认异步实现
- mongodb - MongoDB聚合从包含变量的路径中获取值
- firebase - 如何在firebase中存储用户特定的文档?
- r - 如何根据 R 中的模式创建一个包含部分字符串的新列
- c# - 将对象传递给请求并将属性名称转换为 C# 中的 JsonProperty 名称