0

在“A byte of Python”的帮助下,我刚开始用 Python编程,这个网站对我帮助很大。我的任务是找出一串日语单词中的所有平假名符号。输出应该是一个文本文件,其中所有平假名序列都堆叠在一个列表中。给我的是以下代码:

# -*- coding: utf-8 -*-
import re
import codecs  

prompt = unicode('alotofjapanesesymbols')
out = codecs.open("output.txt", "w", "utf-8")

#Japanese uses 4 major writing systems that are intermixed in Japanese texts.
#Your task is to find all connected sequences of Hiragana symbols in the
# "prompt" Unicode string above and write them out into separate lines
# using the file handle "out".
# The result should be 40 lines looking like this (excluding the # symbol):
#な
#や
#に
#されている
#はその
Etc.

到目前为止,我来到了这个:

# -*- coding: utf-8 -*-

import re
import codecs

s = '受信可能な放送局や、リストに登録されている放送局はその放送局の名前をお使いいただけます 例えばFMヨコハマが受信可能な場合放送局FMヨコハマと言うことでFMヨコハマをお聞きいただけます アドレス帳の検索は音声コマンド登録先を検索のあとに登録されているお名前をお話しください アドレス帳に登録されている方に電話をするときは例えばアドレス帳の鈴木太郎に電話するとお話しください 音声コマンドが認識されにくい場合は同じコマンドを繰り返すかヘルプシステムをご利用ください'

m = re.findall(u'[\u3040-\u309F]', s.decode('utf-8')) # Using regular expressions findall function and unicode of all Hiragana symbols to find them. 

# for char in m:                     
#     print char, hex(ord(char))  # For every character in m, print the character and its unicode using the ord function (unicode is hexadecimal). This is not relevant, but it showed me that i am on the right way.

out = codecs.open( 'output.txt', 'w', 'utf-8' ) 
for char in m:
    out.write(char)
else: 
    out.close()         # Write the Hiragana symbols to a text file as long as there are characters in m, else close file. 

最困难的是整个 unicode-utf-8-encoding-decoding 部分,但我喜欢使用 re.findall 函数和平假名的 unicode 找到平假名符号后的感觉。我现在面临的问题是,我有一个包含所有符号的文本文件,但它们显示在字符串中而不是列表中。我做错了什么,你能帮帮我吗?我已经经历了几个主题,但这对我没有帮助。感谢您在本网站上给我的帮助。

扎瓦蓬加

4

1 回答 1

0

编写时编码为 JSON。

json.dump(m, out)

此外,使用unicode文字以避免解码。

chars = u'あいうえお'
于 2012-09-05T00:20:15.283 回答