我正在使用 WC -l 来计算文本文档中的行数。但是我在这里遇到了问题。
我得到了一个 python 代码,它将不同的数字组合写入不同的文件。每个文件在单独的行上包含每个组合的数字。
当我为它使用 wc -l 时,它没有计算最后一行!
下面是python代码:
import os
import itertools
lst = [6,7,8,9,12,19,20,21,23,24,26,27,28,29,43,44]
combs = []
for i in xrange(1, len(lst)+1):
els = [list(x) for x in itertools.combinations(lst, i)]
combs.extend(els)
for combination in els:
combination_as_strings = map(str, combination)
filename = "_".join(combination_as_strings) + ".txt"
filename = os.path.join("Features", filename)
with open(filename, 'w') as output_file:
output_file.write("\n".join(combination_as_strings))
提前致谢
艾哈迈德
请您参考如下方法:
您正在使用的 join 是在行之间放置换行符,但不是在最后一行的末尾。因此,wc 不计算最后一行(它计算换行符的数量)。
在脚本末尾的 with 子句中添加 output_file.write("\n"):
with open(filename, 'w') as output_file:
output_file.write("\n".join(combination_as_strings))
output_file.write("\n")
