python - 将行添加到现有文件中

标签 python python-2.x

我想将值写入文件中。我在不同的 if 语句中获得两个值。如何修改它以获取同一输出文本文件中的两个值?

if row == 0 and col == 1:
    print ertek
    with open("Output.txt", "w") as kiiras:
        kiiras.write("data1: %s" % ertek)
        kiiras.write("\n")

if type(pattern_colour) is tuple and row == 2:
    tipus = thecell.value
    tipus = tipus.encode('ascii', 'ignore').decode('ascii')
    print tipus
    text_file = open("Output.txt", "w")
    text_file.write("data2: %s" % tipus)

最佳答案

这可能是你的打开方式w的问题,打开文件时应该尝试使用a方式,避免覆盖,

我链接了这个问题,因为它比我的解释要好得多 python open built-in function: difference between modes a, a+, w, w+, and r+?

关于python - 将行添加到现有文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55138161/

相关文章:

Python 字符串编码

python - 在 matplotlib 中创建一个具有上下边缘的矩形面片

python - 生成器作为函数参数

python - 将左侧列化的列化行格式化为列化数据?

python-2.x - Node-sass/sass-loader 替代方案

python - 如何将元素添加到 OrderedDict 的开头?

python - 十进制转二进制函数,Python

python - 显示文件和大小慢命令

python - Crontab 不会运行 python 脚本

python - 我想检查输入是否是 python 代码