小编Tyr*_*ion的帖子

“utf-8-sig”是否适合解码UTF-8和UTF-8 BOM?

我正在使用 Python CSV 库读取两个 CSV 文件。

一种是用UTF-8-BOM编码,另一种是用UTF-8编码。在我的实践中,我发现这两个文件都可以通过使用“utf-8-sig”作为编码类型来读取:

from csv import reader 
with open(file_path, encoding='utf-8-sig') as csv_file:
    c_reader = reader(csv_file, delimiter=',')
    headers = next(c_reader)    
    for row in c_reader:
        print(row)
Run Code Online (Sandbox Code Playgroud)

我想确认一下,“utf-8-sig”是否适合解码UTF-8和UTF-8 BOM?我使用的是 Python 3.6 和 3.7 版本。感谢您的回答!

python csv utf-8 character-encoding

4
推荐指数
1
解决办法
6555
查看次数

alembic 自动生成创建空迁移但生成表

我按照Alembic 的文档自动生成迁移。我的项目结构如下所示:

alembic/
  versions/
  env.py
  README
  script.py.mako
data/
  __init__.py
  db.py
  models.py
alembic.ini
app.db
Run Code Online (Sandbox Code Playgroud)

env.py我完全按照文档进行了更改:

from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context
from data.models import Base

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)

# add your model's …
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy-migrate alembic

1
推荐指数
1
解决办法
3189
查看次数