【发布时间】:2013-06-23 22:27:52
【问题描述】:
我有一些可以在 Python 3 中运行的代码,但我需要将其降级到 Python 2。我有一个类可以编写一个 csv 来显示已生成的随机 ASCII 字符串。这是工作的 Python 3 代码。
file = open(output_table, 'w')
header = 'Path Type Original Attempt Attempt_Length Final Time_1 Time_2 Time_3'.split()
filewriter = csv.writer(self.file, quoting=csv.QUOTE_ALL, delimiter='ę', quotechar='æ')
问题是,如果我使用 ascii 字符,我有可能会导致栏目被关闭并且我的脚本会崩溃。如何使用 Unicode/UTF-8 字符分隔列?
编辑: 所以我找到了一些让 Python 2 更好地使用 utf-8 的方法。包括
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
文件顶部的帮助。我仍然收到 TypeError:“delimiter”必须是 1 个字符的字符串。
【问题讨论】:
标签: csv python-2.7 utf-8