Python:将列表写入文本文件并显示为列表

Python:将列表写入文本文件并显示为列表,python,Python,我刚开始在Python的帮助下编程,这个网站对我帮助很大。我的任务是找到日语单词串中的所有符号。 输出应该是一个文本文件,所有平假名序列堆叠在一个列表中。 给我的是以下代码: # -*- coding: utf-8 -*- import re import codecs prompt = unicode('alotofjapanesesymbols') out = codecs.open("output.txt", "w", "utf-8") #Japanese uses 4 major

我刚开始在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编码-解码部分,但我喜欢在我发现如何使用re.findall函数和平假名的unicode查找平假名符号后的感觉。 我现在面临的问题是,我有一个包含所有符号的文本文件,但它们显示在字符串中,而不是列表中。我做错了什么,你能帮我吗? 我已经讨论了几个话题,但这对我没有帮助。 谢谢你在这个网站上给我的帮助


zawaponga在编写时编码为JSON

json.dump(m, out)
另外,为了避免解码

chars = u'あいうえお'

out.write(char+“\n”)
addendlines。。。write不会隐式添加它们我会使用
out.write(“\n.join(m)+”“\n”)
而不是在循环中写入。谢谢Ansgar,这对我很有效,但只有在我用Word而不是在文本编辑器中打开文件时才有效。不知怎的,word正确地使用了utf-8。谢谢你的回答,我使用了json,并且没有忘记导入json。但是在输出文件中,它仍然没有从上到下列出。Json对我来说不起作用,但是感谢您提供使用unicode文本的提示。我没有使用json,而是使用了Ansgar Wiechers的答案。