我有一个这样的2D数组:
[["Hello", "World"], ["How", "are", "you?"], ["Bye" "World"]]
我想把它放到一个文本文件里,像这样:
Hello World How are you? Bye World
我不知道该怎么做,请有人帮忙。
w46czmvw1#
这是你要找的吗?
text = [["Hello", "World"], ["How", "are", "you?"], ["Bye", "World"]] with open('test.txt', 'w+') as f: for outer in text: for i, inner in enumerate(outer): if i == (len(outer) - 1): f.write(f'{inner}') else: f.write(f'{inner} ') f.write('\n')
5q4ezhmt2#
这是一个更短更有效的替代方案。
text = [["Hello", "World"], ["How", "are", "you?"], ["Bye", "World"]] with open('test.txt','w+') as file: file.write('\n'.join(map(' '.join,text)))
2条答案
按热度按时间w46czmvw1#
这是你要找的吗?
5q4ezhmt2#
这是一个更短更有效的替代方案。