小编dar*_*rdi的帖子

在python中编写一个新的文本文件

我正在编写遍历文本文件的代码,计算每行中有多少单词,并且无法将结果(每行包含一个数字的多行)放入新的文本文件中.

我的代码:

in_file = open("our_input.txt")
out_file = open("output.txt", "w")


for line in in_file:
    line = (str(line)).split()
    x = (len(line))
    x = str(x)
    out_file.write(x)

in_file.close()  
out_file.close()
Run Code Online (Sandbox Code Playgroud)

但是我得到的文件将所有数字放在一行中.

如何在我正在制作的文件中分隔它们?

python split file python-3.x

2
推荐指数
1
解决办法
608
查看次数

标签 统计

file ×1

python ×1

python-3.x ×1

split ×1