Python 将列表写入带格式的文本文档
Python Writing a List into a Text Document With formatting
我需要能够 python 创建一个以特定方式显示列表的新文本文档,但我不确定如何在文本创建区域内使用格式
def write_to_file(filename, character_list):
### Using a while loop to iterate over the list of lists (characters).
index = 0
while index < len(character_list):
with open("new_characters.txt", "w") as output:
output.write(str(character_list))
index = index + 1
上面的代码是我为在文本文档中显示完整列表所做的代码,但它只是将所有内容放在一行中。
我需要这样设置:
神奇女侠
戴安娜·普林斯
小时 5 5 0 0 90
蝙蝠侠
布鲁斯·韦恩
小时 6 2 0 4 80
而不是:
[['Wonder Woman', 'Diana Prince', 'h', 5, 5, 0, 0, 90], ['Batman', 'Bruce Wayne', 'h', 6, 2, 0, 4, 80]
,
这是上面发布的代码的输出。
并且代码必须在循环中!
试试这个方法:-
- For 循环是更好的选择
- 使用
\n
换行
def write_to_file(filename, character_list):
with open(f"{filename}.txt", "w") as output:
for characters in character_list:
for character in characters:
character =str(character)
output.write(character+("\n" if len(character)>1 else "" ))
#output.write(character+("\n" if len(character)>1 else " " )) for --> h 5 5 0 0 9 0
write_to_file('Any',[['Wonder Woman', 'Diana Prince', 'h', 5, 5, 0, 0, 90], ['Batman', 'Bruce Wayne', 'h', 6, 2, 0, 4, 80]])
输出:
Wonder Woman
Diana Prince
h550090
Batman
Bruce Wayne
h620480
试试这个。
def write_to_file(filename, character_list):
### Using a while loop to iterate over the list of lists (characters).
index = 0
while index < len(character_list):
with open("new_characters.txt", "w") as output:
for item in character_list:
for character in item:
output.write(str(character) + '\n')
index = index + 1
这适用于您以要求的格式显示的子集。
def write_to_file(filename, character_list):
# open file with given filename, in 'write' mode
with open(filename, 'w') as f:
# iterate over characters in for loop
# using tuple unpacking
for (hero_name, char_name, *data) in character_list:
# write hero and character names on a line each
f.write(hero_name + '\n') # e.g. 'Wonder Woman'
f.write(char_name + '\n') # e.g. 'Diana Prince'
# convert all remaining elements to a string
# using list comprehension
data = [str(i) for i in data]
# create a single string from a list of values separated by a space
# using string join method on the list
data = ' '.join(data)
# write to file with newline
f.write(data + '\n') # e.g. 'h 5 5 0 0 90'
其中的关键组成部分是 tuple unpacking, list comprehensions, and the string join method。我还包括了在打开文件时实际使用的文件名参数的使用。这意味着如果您还没有将带有扩展名的文件名传递给函数调用。
我需要能够 python 创建一个以特定方式显示列表的新文本文档,但我不确定如何在文本创建区域内使用格式
def write_to_file(filename, character_list):
### Using a while loop to iterate over the list of lists (characters).
index = 0
while index < len(character_list):
with open("new_characters.txt", "w") as output:
output.write(str(character_list))
index = index + 1
上面的代码是我为在文本文档中显示完整列表所做的代码,但它只是将所有内容放在一行中。
我需要这样设置:
神奇女侠
戴安娜·普林斯
小时 5 5 0 0 90
蝙蝠侠
布鲁斯·韦恩
小时 6 2 0 4 80
而不是:
[['Wonder Woman', 'Diana Prince', 'h', 5, 5, 0, 0, 90], ['Batman', 'Bruce Wayne', 'h', 6, 2, 0, 4, 80]
,
这是上面发布的代码的输出。
并且代码必须在循环中!
试试这个方法:-
- For 循环是更好的选择
- 使用
\n
换行
def write_to_file(filename, character_list):
with open(f"{filename}.txt", "w") as output:
for characters in character_list:
for character in characters:
character =str(character)
output.write(character+("\n" if len(character)>1 else "" ))
#output.write(character+("\n" if len(character)>1 else " " )) for --> h 5 5 0 0 9 0
write_to_file('Any',[['Wonder Woman', 'Diana Prince', 'h', 5, 5, 0, 0, 90], ['Batman', 'Bruce Wayne', 'h', 6, 2, 0, 4, 80]])
输出:
Wonder Woman
Diana Prince
h550090
Batman
Bruce Wayne
h620480
试试这个。
def write_to_file(filename, character_list):
### Using a while loop to iterate over the list of lists (characters).
index = 0
while index < len(character_list):
with open("new_characters.txt", "w") as output:
for item in character_list:
for character in item:
output.write(str(character) + '\n')
index = index + 1
这适用于您以要求的格式显示的子集。
def write_to_file(filename, character_list):
# open file with given filename, in 'write' mode
with open(filename, 'w') as f:
# iterate over characters in for loop
# using tuple unpacking
for (hero_name, char_name, *data) in character_list:
# write hero and character names on a line each
f.write(hero_name + '\n') # e.g. 'Wonder Woman'
f.write(char_name + '\n') # e.g. 'Diana Prince'
# convert all remaining elements to a string
# using list comprehension
data = [str(i) for i in data]
# create a single string from a list of values separated by a space
# using string join method on the list
data = ' '.join(data)
# write to file with newline
f.write(data + '\n') # e.g. 'h 5 5 0 0 90'
其中的关键组成部分是 tuple unpacking, list comprehensions, and the string join method。我还包括了在打开文件时实际使用的文件名参数的使用。这意味着如果您还没有将带有扩展名的文件名传递给函数调用。