「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 を使ってひらがな記号を見つける方法を見つけたときの感覚が気に入りました。私が今直面している問題は、すべてのシンボルを含むテキスト ファイルがあることですが、それらはリストではなく文字列で表示されます。私は何を間違っていますか?助けてもらえますか?すでにいくつかのトピックを調べましたが、それは役に立ちませんでした。このサイトですでに助けてくれてありがとう。
ざわぽんが