0

odfpy モジュールを使用して ods (Opendocument スプレッドシート) ドキュメントから読み取ろうとしています。これまでのところ、いくつかのデータを抽出することができましたが、セルに非標準の入力が含まれている場合は常に、スクリプトで次のエラーが発生します。

Traceback (most recent call last):
File "python/test.py", line 26, in <module>
 print x.firstChild
File "/usr/lib/python2.7/site-packages/odf/element.py", line 247, in __str__
 return self.data.encode()
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0105' in position 4: ordinal not in range(128)

出力でエンコーディングを強制しようとしましたが、明らかに印刷ではうまくいきません:

Traceback (most recent call last):
  File "python/test.py", line 27, in <module>
   print x.firstChild.encode('utf-8', 'ignore')
AttributeError: Text instance has no attribute 'encode'

ここでの問題は何ですか?モジュールコードを編集せずにどのように解決できますか? 出力でエンコードを実行する代わりに機能するものはありますか?

これが私のコードです:

from odf.opendocument import Spreadsheet
from odf.opendocument import load
from odf.table import Table,TableRow,TableCell
from odf.text import P
import sys,codecs
doc = load(sys.argv[1])
d = doc.spreadsheet
tables = d.getElementsByType(Table)
for table in tables:
  tName = table.attributes[(u'urn:oasis:names:tc:opendocument:xmlns:table:1.0', u'name')]
  print tName
  rows = table.getElementsByType(TableRow)
  for row in rows[:2]:
    cells = row.getElementsByType(TableCell)
    for cell in cells:
      tps = cell.getElementsByType(P)
      if len(tps)>0:
        for x in tps:
          #print x.firstChild
          print x.firstChild.encode('utf-8', 'ignore')
4

2 に答える 2

1

最新の を使用していない可能性がありますodfpy。最新バージョンでは、 の__str__メソッドTextは次のように実装されています。

def __str__(self):
    return self.data

最新バージョンに更新odfpyし、コードを次のように変更します。

print x.firstChild.__str__().encode('utf-8', 'ignore')

アップデート

Textこれは、 :の生の Unicode データを取得する別の方法です__unicode__。したがって、更新したくない場合はodfpy、コードを次のように変更します。

print x.firstChild.__unicode__().encode('utf-8', 'ignore')
于 2015-07-07T09:08:04.957 に答える
0

ライブラリ自体が呼び出しているようですencode()-

return self.data.encode()

これは、システムのデフォルトの encoding を使用します。これは、あなたの場合はascii. -を使用して確認できます

import sys
sys.getdefaultencoding()

トレースバックから、実際のデータは という変数に存在するようですdata

代わりに以下を試してください-

print x.firstChild.data
于 2015-07-07T08:53:50.827 に答える