【问题标题】:self.writer.writerow([s.encode('utf-8') for s in row])self.writer.writerow([s.encode('utf-8') for s in row])
【发布时间】:2011-10-31 05:32:59
【问题描述】:

我正在尝试写入一个文件(在 Python 中),但它说:

'ManyRelatedManager' 对象没有属性'encode'

代码如下:

self.writer.writerow([s.encode('utf-8') for s in row])

有没有办法避免/解决这个问题?

【问题讨论】:

  • @SimpleCoder: csv 模块不支持 unicode -> docs.python.org/library/csv.html
  • 是的,但它说:“因此,所有输入都应该是 UTF-8 或可打印的 ASCII 以确保安全;...”这是我的情况。问题是我的 's' 对象不是 unicode 对象,我应该将其更改为 unicode 对象。
  • 问题是:使用此代码可以正常工作:w.writerow([u"%d"%s.id,s.name,u"%d"%s.clicks,s .status,s.stand.nick]) 但不是这个: w.writerow([u"%d"%s.id,s.name,u"%d"%s.clicks,s.status,s .stand.nick,s.studiengaenge]) 因为 'studiengaenge' 据称不是 unicode 对象。这会是我的问题的原因吗..?

标签: python


【解决方案1】:

在你的代码中

self.writer.writerow([s.encode('utf-8') for s in row])

“s”对象可能不是 unicode 对象 您应该先尝试将“s”对象更改为 unicode

【讨论】:

    猜你喜欢
    • 2013-11-11
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多