打印多个txt文件行

时间:2018-04-25 16:16:59

标签: python text

我正在尝试使用Python从* .txt文件中打印多行(在本例中为2行)。

我想在同一行打印“if”,但我不能。

代码:

import os
import sys

path = #sys.argv[1]
InputText1 = '#  Class ID     Number of Cells    Class Name'
InputText2 = '# Means'


for file in os.listdir(path):
    #print file [13:15]
    #if file.endswith("*.gsg"):
        #print os.path.join(path,file)
    with open(os.path.join(path,file), "r") as input:
        for line in input:
            if InputText1 in line:
                #print(line)
                print str(file)[0:1].strip() + "," + str(next(input)[:10].strip()) + ","
            if InputText2 in line:
                #print(line)
                print next(input).strip()

结果:

1,1,
5.004188e-001
1,2,
5.822568e-001
1,3,
6.339202e-001
1,4,
6.711560e-001
1,5,
6.976744e-001

0 个答案:

没有答案