【发布时间】:2019-01-30 13:51:56
【问题描述】:
我有一个包含 40 个单词的文本文件,例如one two three ... forty。所有单词相互之间,不包括逗号。我需要将它们打印到屏幕或彼此相邻的另一个文件中,用逗号分隔(例如:one, two, three, ...),并让它们每十 (10) 个或七 (7) 个单词包装一次。
这是我的代码,无法正常工作:
import textwrap
flag = 1
comma = ', '
with open('drop_words.txt', encoding='utf-8') as file:
content = file.read()
content = content.split()
words = comma.join(content)
if len(content)%7 == 0:
print(words, '\n')
有人可以帮忙吗? 谢谢。
【问题讨论】:
-
您能提供一个示例 I/O 吗?
-
等等。 and also have them wrap up 每十 (10) 或七 (7) 个字。 是什么意思?
-
输出应该是:一、二、三、四、五、六、七、八、九、十(第一行),然后:十一、十二、...二十(第二行)行),依此类推...您的代码 user5173426 提供的不起作用。
-
编辑了我的答案。
标签: python string printing line