如何将代码输出逐行读取和写入代码到文本文件中

时间:2019-06-17 17:49:22

标签: python python-3.x

例如,我的文本文件中的列表如下所示:

1,2.12,3.123

然后将其打印到我的输出文本文件(每个数字的小数位):

Line 1: INVALID VALID INVALID

但是,如果我的文本文件中的列表是这样的:

1,2.12,3.123
1,1.00

然后将其打印到我的输出文本文件中:

Line 1: Line 2:   INVALID 
VALID
INVALID
Line 3: Line 4:   INVALID
VALID
INVALID

如何将其打印到我的输出文本文件中?

Line 1: INVALID VALID INVALID
LINE 2: INVALID VALID

我试图添加一个索引=索引+1,以便它可以为每行添加1。

from functools import reduce
res = 0
outfile = "output2.txt"
baconFile = open(outfile,"wt")
index = 0
invalid_string = "INVALID"
valid_string = "VALID"
for line in open("file.txt"):  # read file line-by-line
        carrera = ''
        index = index +1       # Count lines that contain what the command wants
        print("Line {}: ".format(index))
        baconFile.write("Line {}:  ".format(index))

    with open('file.txt') as file:
        number_list = file.readline().strip().split(',')
        for line in number_list:
            if len(line.split('.')[-1]) == 2:
                # res += 1
##              print("VALID")

                carrera = valid_string 
            if len(line.split('.')[-1]) != 2:
                #res += 1
                carrera = invalid_string  

            print (carrera)
            baconFile.write(carrera + " ")
# print(res)
baconFile.close()

预期:

Line 1: INVALID VALID INVALID
LINE 2: INVALID VALID

实际:

Line 1: Line 2:   INVALID 
    VALID
    INVALID
    Line 3: Line 4:   INVALID
    VALID
    INVALID

2 个答案:

答案 0 :(得分:1)

使用for line in file遍历文件中的每一行,并使用print('...', end='')进行不带换行符的打印。请勿两次open()

res = 0
outfile = "output2.txt"
baconFile = open(outfile,"wt")
index = 0
invalid_string = "INVALID"
valid_string = "VALID"
index = 1
with open('file.txt') as file:
    for line in file:
        print("Line {}: ".format(index), end='')
        baconFile.write("Line {}:  ".format(index))
        number_list = line.strip().split(',')
        for number in number_list:
            if len(number.split('.')[-1]) == 2:
                #res += 1
##              print("VALID")

                carrera = valid_string 
            if len(number.split('.')[-1]) != 2:
                #res += 1
                carrera = invalid_string  

            print (carrera, end='')
            baconFile.write(carrera + " ")
        print('\n', end='')
        baconFile.write('\n')
        index += 1

(对于其他回答者,我避免使用enumerate,因为代码中已经包含index。)

答案 1 :(得分:1)

我对格式进行了一些修改:

res = 0
outfile = "output2.txt"
baconFile = open(outfile,"wt")
index = 0
invalid_string = "INVALID"
valid_string = "VALID"
with open('file.txt') as file:
    for line in file:
        index = index +1
        o = "Line {}: ".format(index)
        number_list = line.strip().split(',')
        for x in number_list:
            if len(x.split('.')[-1]) == 2:
                o += valid_string + " "
            if len(x.split('.')[-1]) != 2:
                o += invalid_string + " "

        print(o)
        baconFile.write(o + "\n")
baconFile.close()

编辑:不小心将'carrera'变量留在那里,以为我可以将其编辑掉,因为它已不再有用