English 中文(简体)
问题7. 如何把MeCab的信息写到文字档案中?
原标题:Python 2.7 - how do you write MeCab parsed information to a text file?

我写了一份《全球倡议》,允许日本提出意见,而且当你去“档案”时,在文字档案中写明。 然后,该文本文件通过MeCab操作,在文字之间放置空间。 之后应再次提交文本档案,以便存放在另一个GUI窗口。

问题Im是,它不想在文本档案中写下分类的数据。 它没有第一次提出问题。 此外,它还向IDLE印刷了斜线,没有问题。 此处为斜线和错误:

#!/usr/bin/python
# -*- coding: <utf-8> -*-
import sys

import MeCab
import codecs

read_from = open("pholder.txt").read()
mecab = MeCab.Tagger("-Owakati")
output = mecab.parse(read_from)
print output


text = output
write_to = codecs.open("pholder.txt", "w", "utf-8")
write_to.write(text)
write_to.close()

Traceback (most recent call last):
  File "C:...mecabSpaces.py", line 16, in <module>
    write_to.write(text)
  File "C:...codecs.py", line 691, in write
    return self.writer.write(data)
  File "C:...codecs.py", line 351, in write
    data, consumed = self.encode(object, self.errors)
UnicodeDecodeError:  ascii  codec can t decode byte 0xe4 in position 0: ordinal not in range(128)
最佳回答

定额数据为t unicode,它带有星体。

因此,当你试图将数据写到档案中时,它试图在encoding之前将其编码到utf-8。 由于您的缺省代码c为ascii,但您实际上有utf-8<>>/code>,因此,该代码按位次值计算为128或以上。

您应为.decode(utf-8 )

问题回答

Here s working code. Thanks to agf for helping me pull my head out of my butt.

#!/usr/bin/python
# -*- coding: <utf-8> -*-
import MeCab

read_from = open("pholder.txt", "r").read()
mecab = MeCab.Tagger("-Owakati")
output = mecab.parse(read_from)
print output

text = output
write_to = open("pholder.txt", "w").write(text)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...