将文本文件中的行数相乘--需要修复。

问题描述 投票:1回答:1

我得到了一个在文本文件中乘行的代码。


这是我的基本文本文件

Text_1. txt (Open_File):

1
2
3
4

代码。


file = Open_file

with open(file, "r") as f:
    file = f.read()

file_multiply = file * 3

with open('multiply.txt', 'w') as outfile:
    outfile.write(file_multiply)


问题是multiply.txt(输出文件)是这样的。

1
2
3
41
2
3
41
2
3
4

(问题是 "1 "不在行首)你有办法解决吗? 我提供了代码来帮助你

python python-3.x multiplying
1个回答
3
投票

你需要在你写的每一项之间加上换行符。

with open('multiply.txt', 'w') as outfile:
    outfile.write('\n'.join([file]*3))
© www.soinside.com 2019 - 2024. All rights reserved.