python - 如何将字符串列表写入文件并添加换行符?

标签 python file-io newline

考虑:

def generator():
    nums = ['09', '98', '87', '76', '65', '54', '43']
    s_chars = ['*', '&', '^', '%', '$', '#', '@',]
  
    data = open("list.txt", "w")
    for c in s_chars:
        for n in nums:
            data.write(c + n)
    data.close()

我想在每个“c + n”之后添加一个换行符。

最佳答案

改变

data.write(c + n)

data.write("%s%s\n" % (c, n))

关于python - 如何将字符串列表写入文件并添加换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5429064/

相关文章:

html - 使用 <pre><code> 标签隐藏/删除 html 代码段的第一个换行符/空行

python - 如何在给定的时间间隔内启用/禁用按钮

python - Python 的数字字典

c - 使用 C 中的排序字典对字符串进行拼写检查

java - 无法使用对象列表写入/读取文件

java - 从多个线程可靠地写入一个 PrintWriter

Python3.4 : for each line in text

python - 多态函数的功能注释

C# 替换字符串中的特殊字符

javascript - JS中的换行\n问题