[英]Python: Write a list to a text file and show as a list
我刚开始在“一个字节的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找到平假名符号后,我很喜欢这种感觉。 我现在面临的问题是,我有一个包含所有符号的文本文件,但是它们以字符串而不是列表的形式显示。 我做错了什么,您能帮帮我吗? 我已经讨论了几个主题,但这对我没有帮助。 感谢您在此站点上为我提供的帮助。
扎瓦蓬加
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.