Python 从 2.7 升级到 3.7:unicode 错误

Python upgrade from 2.7 to 3.7 : unicode error

我正在将我的 python 代码从 2.7 更新到 3.7。 基本上,我正在尝试 运行 google 数据流上的管道,它从大查询视图读取数据并转换它,然后在另一个 table.[=14= 中写回大查询]

但是,如果我使用的是 unicode 错误,则更新时:NameError: name 'unicode' is not defined

            bq_source = beam.io.BigQuerySource(query=query, use_standard_sql=True)
            records = (pipeline
                      | 'Read %s From BQ' % v.get('name') >> beam.io.Read(bq_source)
                      | 'BQ Create KV %s' % count >> beam.Map(lambda row: (row['value'].encode("utf-8"),
                                                                            {unicode(key).encode("utf-8"): unicode(
                                                                                value).encode("utf-8")
                                                                            for key, value in row.items()}))
                      | 'BQ Group By Key %s' % count >> beam.GroupByKey()
                      | 'BQ Calculate %s  Score' % v.get('name') >> beam.ParDo(ProcessDataDoFn(),
                                                                                    filter_id=v.get('filter_id'),
                                                                                    date=date)
                      )

如果我运行在 python 2.7 中使用与上面相同的代码,它 运行 没问题。

一段时间后,当我在 python 3+ as str 中读取 unicode 时,我尝试更新代码 - 如果我更新我的代码以将 unicode 替换为 str。来自大查询的文件未被读取,因此导致稍后出现密钥错误:

            bq_source = beam.io.BigQuerySource(query=query, use_standard_sql=True)
            records = (pipeline
                      | 'Read %s From BQ' % v.get('name') >> beam.io.Read(bq_source)
                      | 'BQ Create KV %s' % count >> beam.Map(lambda row: (row['value'].encode("utf-8"),
                                                                            {str(key).encode("utf-8"): str(
                                                                                value).encode("utf-8")
                                                                            for key, value in row.items()}))
                      | 'BQ Group By Key %s' % count >> beam.GroupByKey()
                      | 'BQ Calculate %s Score' % v.get('name') >> beam.ParDo(ProcessDataDoFn(),

编辑 1:

无需编码即可更新代码 - 现在可用。

bq_source = beam.io.BigQuerySource(query=query, use_standard_sql=True)
                records = (pipeline
                           | 'Read %s From BQ' % v.get('name') >> beam.io.Read(bq_source)
                           | 'BQ Create KV %s' % count >> beam.Map(lambda row: (row['value'],
                                                                                {key:
                                                                                     value
                                                                                 for key, value in row.items()}))
                           | 'BQ Group By Key %s' % count >> beam.GroupByKey()
                           | 'BQ Calculate %s  Score' % v.get('name') >> beam.ParDo(ProcessDataDoFn(),
                                                                                         filter_id=v.get('filter_id'),
                                                                                         date=date)
                           )
s = 'hello'
u = u'hello'
b = u.encode('utf-8')
print (type(s), type(u), type(b))

在python38

<class 'str'> <class 'str'> <class 'bytes'>

在python27

(<type 'str'>, <type 'unicode'>, <type 'str'>)

该转换的目的显然是将 unicode 转换为 str,这在 python3 中不再是相关问题。相反,我们将其更改为不兼容的字节。简单地不编码,并使用 str(key) - 或者只是键,如果你已经知道它是 unicode。