【发布时间】:2017-12-12 00:57:39
【问题描述】:
正如问题所述,我不明白为什么输出是两行而不是一行。这是我的代码:
import os
items=os.listdir("C:/output")
for names in items:
if names.endswith(".txt"):
with open('C:/output/' + names, encoding="utf8") as currentFile:
for i, line in enumerate(currentFile):
if ('Date Released' in line):
print(line + names)
else:
i = i + 1
if i > 11:
break
我尝试在 print 语句中使用逗号,如下所示:print(line, names),但不起作用。我也试过在 print 语句的末尾使用 .format(line, names) ,也没有运气。
输出如下:
Date Released: 12/14/2016
1393-004IP_ B_ C2 filename.txt
Date Released: 4/11/2017
1476-002 IP, filename.txt
Date Released: 9/25/2015
1987-XXX IP_B_filename.txt
Date Released: 4/9/2015
2044-002IP_A_C1 filename.txt
等等
在同一行中获取日期和文件名会非常有帮助,这样我就可以很容易地将它放入 Excel。感谢您的任何意见。
【问题讨论】:
标签: python python-3.x printing output