不同块进程的Python打印问题

2024-04-19 06:18:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我的剧本有问题,需要你的帮助。 我可能没有用正确的措辞来解释,但我希望它能足够清楚。你知道吗

我有3个文件,file1.txt file2.txt和file3.txt,当每个文件中的第1行对应于同一个对象时。你知道吗

$ cat file1.txt 
server1, windows, 1, 120, running
server2, linux, 1, 250, offline
server3, centos, 0, 60, maintenance
server4, windows, 1, 123, running
server5, linux, 0, 145, offline
server6, windows, 1, 123, running
server7, linux, 0, 145, offline
server8, windows, 1, 123, running
server9, windows, 1, 123, running

$ cat file2.txt 
1, 1, 1, 1, 34, 1, 1, 1, 97, 1, 1, 1, 1
101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
0, 0, 23, 0, 1, 0, 0, 0, 23, 0, 0, 0, 0
1, 21, 1, 1, 34, 1, 1, 1, 1, 1, 1, 1, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
101, 73, 101, 101, 101, 101, 101, 101, 101, 1, 14, 101, 101
13, 101, 101, 101, 101, 0, 101, 101, 101, 101, 101, 101, 101
101, 101, 101, 6, 101, 101, 22, 101, 101, 101, 11, 101, 101

$ cat file3.txt 
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1
21, 0, 0, 0, 0, 0, 70, 0, 0, 0, 45, 0, 0
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
0, 0, 0, 0, 0, 20, 0, 40, 0, 0, 0, 0, 0
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101
101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101, 101

我有以下脚本:

chance = ''
myliststatus = ''

with open('file2.txt', 'r') as f2:
    file2 = f2.read().splitlines()
with open('file3.txt', 'r') as f3:
    file3 = f3.read().splitlines()

varfiles2 = [[int(x) for x in y.split(', ')] for y in file2]
varfiles3 = [[int(x) for x in y.split(', ')] for y in file3]

for varfile2, varfile3 in zip(varfiles2, varfiles3):
    for fi2,fi3 in zip(varfile2, varfile3):
        if ( 0 < fi2 < 100 and fi3 == 0):
            chance = "Low"
        elif (fi2 == 0 and 0 < fi3 < 100):
            chance = "high"
        elif ( fi2 == 0 and fi3 == 0):
            chance = "medium"
        else:
            chance = "null"
    print chance

with open("file1.txt") as f1:
    for line in f1.readlines():
        mylist = line.split(", ")
        if mylist[2] == "0":
            myliststatus = "staging"
        elif mylist[2] == "1":
            myliststatus = "Production"
        else:
            myliststatus = "decommissioned"

        print myliststatus

我需要的是能够在同一行中打印chance和myliststatus的结果,因此第1行的chance和myliststatus的结果打印在同一行中,并且输出应该是9行2列,如下所示:

print chance, myliststatus

null, Production
null, Production
null, staging
medium, Production
null, staging
null, Production
null, staging
null, Production
null, decommissioned

但是因为缩进与我的两个块不同,如果我改变其中一个块的缩进,结果就不一样了。你知道吗

对于这两个块,最好的方法是什么?如果我还需要添加其他块?你知道吗

非常感谢

更新

chance = 'tbd'

s1 = ["S", "stime", "M", "mtime","T", "ttime","W", "wtime","Th", "thtime", "F", "ftime", "Sa"]
static = "S"

with open("file1.txt") as f1:
    p1 = f1.read().splitlines()
with open('file2.txt', 'r') as f2:
    bgfile2 = f2.read().splitlines()
with open('file3.txt', 'r') as f3:
    bgfile3 = f3.read().splitlines()


varbgfiles2 = [[int(x) for x in y.split(', ')] for y in bgfile2]
varbgfiles3 = [[int(x) for x in y.split(', ')] for y in bgfile3]


for varp1, varbgfile2, varbgfile3 in zip(p1, varbgfiles2, varbgfiles3):
    for p1,f,i,s in zip(p1, varbgfile2, varbgfile3, s1):
        if ( f > 0 and i == 0 and static == s and p1[2] == 1):
            chance = "OK1"
        elif ( f == 0 and i > 0 and static == s and p1[2] == 1):
            chance = "NOK1"
        elif ( f > 0 and i > 0 and static == s and p1[2] == 1):
            chance = "NULL1"
        elif ( f == 0 and i == 0 and static == s and p1[2] == 1):
            chance = "BAD1"
        elif ( f > 0 and i == 0 and static == s and p1[2] == 0):
            chance = "OK0"
        elif ( f == 0 and i > 0 and static == s and p1[2] == 0):
            chance = "NOK0"
        elif ( f > 0 and i > 0 and static == s and p1[2] == 0):
            chance = "NULL0"
        elif ( f == 0 and i == 0 and static == s and p1[2] == 0):
            chance = "BAD0"
        print chance

因此,当static设置为列表s1中的第一列时,我应该为(file1中的line1,2,3)和(file2和file3中的line1,2,3/column1)预期以下内容。你知道吗

 line 1 chance = NULL1
 line 2 chance = NULL1
 line 3 chance = NULL0

我希望这能澄清问题,非常感谢。你知道吗


Tags: andintxtforaswithstaticopen
1条回答
网友
1楼 · 发布于 2024-04-19 06:18:12

听起来你想把3个文件中的信息合并起来。您正在使用zip组合来自2个文件的信息,但是zip可以接受任意数量的iterables—如果您将3个序列传递给它,它将返回一个3元组的迭代器。你知道吗

import csv
f1 = csv.reader(open('file1.txt'))
f2 = csv.reader(open('file2.txt'))
f3 = csv.reader(open('file3.txt'))

for part1, part2, part3 in zip(f1, f3, f3):
    part2 = map(int, part2)
    part3 = map(int, part3)
    # process part1, 2 and 3 at the same time.

相关问题 更多 >