Python代码分步运行而不是整体运行

时间:2020-01-03 12:38:23

标签: python python-3.x

我有一些代码可以读取成绩单并搜索存储在不同文本文件中的短语列表。如果找到匹配项,它将打印匹配行,然后打印下一行(响应),并将其存储在结果文本文件中。然后再次读取此结果文本文件,并将其放入指定区域的excel文件中。

我将代码工作到可以生成结果并将其放入excel电子表格的位置。 结果中的数据没有更新。我让代码从10个列表中随机选择一个成绩单以分散结果,但是我必须一遍又一遍地运行整个代码以便更新所有5个类别的结果。这五个类别是:

B / 一种 / N / T / P

有人知道我在哪里错吗?甚至是如何让所有分析部分同时运行而不是一个一个地运行?

我的代码如下:

import sys
import os, random
random_file=random.choice(os.listdir("/Transcripts/"))

file = ("Transcripts/" + (random_file))


#Budget Analysis
sep = '-' * 30 + '\n'
with open(file) as t_file,\
    open ('b_result.txt') as b_result_file,\
    open('BANTP Questions/B.txt') as b_file, \
    open('BANTP Follow Up Questions/B.txt') as b_up_file:
    t_lines = t_file.readlines()
    b_lines = [l.strip() for l in b_file.readlines()]
    f_lines = b_up_file.readlines()
    # Checking each transcription line
    for counter, t_line in enumerate(t_lines):
        # Checking if any P-line is in the T-line
        b_in_t = any([b_line in t_line for b_line in b_lines])
        if b_in_t:
            # Printing Q and A
            answer = t_lines[counter+1]
            print(f'{t_line}{sep}{answer}{sep}', end='', file=open("b_result.txt", "w"))
            # Checking if positive
            if 'yes' in answer.lower() or 'yeah' in answer.lower():
                # Grabbing the next 100 lines to check them for follow-ups
                batch = t_lines[counter+2:min(counter+100, len(t_lines))]
                for index, line in enumerate(batch):
                    # Checking the line if it contains a question
                    f_in_t = any([f_line in line for f_line in f_lines])
                    if f_in_t:
                        # Printing Q and A
                        answer = batch[index+1]
                        print(f'{line}{sep}{answer}{sep}', end='', file=open("b_result.txt", "a"))
                    elif counter % 2 == 0:
                        # No follow-up - move to the next question
                        # Except it's customer's reply
                        break

#Authority Analysis
sep = '-' * 30 + '\n'
with open(file) as t_file,\
    open ('BANTP Results/a_result.txt') as a_result_file,\
    open('BANTP Questions/A.txt') as a_file, \
    open('BANTP Follow Up Questions/A.txt') as a_up_file:
    t_lines = t_file.readlines()
    b_lines = [l.strip() for l in a_file.readlines()]
    f_lines = a_up_file.readlines()
    # Checking each transcription line
    for counter, t_line in enumerate(t_lines):
        # Checking if any P-line is in the T-line
        b_in_t = any([b_line in t_line for b_line in b_lines])
        if b_in_t:
            # Printing Q and A
            answer = t_lines[counter+1]
            print(f'{t_line}{sep}{answer}{sep}', end='', file=open("BANTP Results/a_result.txt", "w"))
            # Checking if positive
            if 'yes' in answer.lower() or 'yeah' in answer.lower():
                # Grabbing the next 100 lines to check them for follow-ups
                batch = t_lines[counter+2:min(counter+100, len(t_lines))]
                for index, line in enumerate(batch):
                    # Checking the line if it contains a question
                    f_in_t = any([f_line in line for f_line in f_lines])
                    if f_in_t:
                        # Printing Q and A
                        answer = batch[index+1]
                        print(f'{line}{sep}{answer}{sep}', end='', file=open("BANTP Results/a_result.txt", "a"))
                    elif counter % 2 == 0:
                        # No follow-up - move to the next question
                        # Except it's customer's reply
                        break



#Need Analysis
sep = '-' * 30 + '\n'
with open(file) as t_file,\
    open ('BANTP Results/n_result.txt') as n_result_file,\
    open('BANTP Questions/N.txt') as n_file, \
    open('BANTP Follow Up Questions/N.txt') as n_up_file:
    t_lines = t_file.readlines()
    b_lines = [l.strip() for l in n_file.readlines()]
    f_lines = n_up_file.readlines()
    # Checking each transcription line
    for counter, t_line in enumerate(t_lines):
        # Checking if any P-line is in the T-line
        b_in_t = any([b_line in t_line for b_line in b_lines])
        if b_in_t:
            # Printing Q and A
            answer = t_lines[counter+1]
            print(f'{t_line}{sep}{answer}{sep}', end='', file=open("BANTP Results/n_result.txt", "w"))
            # Checking if positive
            if 'yes' in answer.lower() or 'yeah' in answer.lower():
                # Grabbing the next 100 lines to check them for follow-ups
                batch = t_lines[counter+2:min(counter+100, len(t_lines))]
                for index, line in enumerate(batch):
                    # Checking the line if it contains a question
                    f_in_t = any([f_line in line for f_line in f_lines])
                    if f_in_t:
                        # Printing Q and A
                        answer = batch[index+1]
                        print(f'{line}{sep}{answer}{sep}', end='', file=open("BANTP Results/n_result.txt", "a"))
                    elif counter % 2 == 0:
                        # No follow-up - move to the next question
                        # Except it's customer's reply
                        break


#Timeline Analysis
sep = '-' * 30 + '\n'
with open(file) as t_file,\
    open ('BANTP Results/t_result.txt') as time_result_file,\
    open('BANTP Questions/T.txt') as time_file, \
    open('BANTP Follow Up Questions/T.txt') as time_up_file:
    t_lines = t_file.readlines()
    b_lines = [l.strip() for l in time_file.readlines()]
    f_lines = time_up_file.readlines()
    # Checking each transcription line
    for counter, t_line in enumerate(t_lines):
        # Checking if any P-line is in the T-line
        b_in_t = any([b_line in t_line for b_line in b_lines])
        if b_in_t:
            # Printing Q and A
            answer = t_lines[counter+1]
            print(f'{t_line}{sep}{answer}{sep}', end='', file=open("BANTP Results/t_result.txt", "w"))
            # Checking if positive
            if 'yes' in answer.lower() or 'yeah' in answer.lower():
                # Grabbing the next 100 lines to check them for follow-ups
                batch = t_lines[counter+2:min(counter+100, len(t_lines))]
                for index, line in enumerate(batch):
                    # Checking the line if it contains a question
                    f_in_t = any([f_line in line for f_line in f_lines])
                    if f_in_t:
                        # Printing Q and A
                        answer = batch[index+1]
                        print(f'{line}{sep}{answer}{sep}', end='', file=open("BANTP Results/t_result.txt", "a"))
                    elif counter % 2 == 0:
                        # No follow-up - move to the next question
                        # Except it's customer's reply
                        break



#Partner Analysis
sep = '-' * 30 + '\n'
with open(file) as t_file,\
    open ('BANTP Results/p_result.txt') as p_result_file,\
    open('BANTP Questions/P.txt') as p_file, \
    open('BANTP Follow Up Questions/P.txt') as p_up_file:
    t_lines = t_file.readlines()
    p_lines = [l.strip() for l in p_file.readlines()]
    f_lines = p_up_file.readlines()
    # Checking each transcription line
    for counter, t_line in enumerate(t_lines):
        # Checking if any P-line is in the T-line
        p_in_t = any([p_line in t_line for p_line in p_lines])
        if p_in_t:
            # Printing Q and A
            answer = t_lines[counter+1]
            print(f'{t_line}{sep}{answer}{sep}', end='', file=open("BANTP Results/p_result.txt", "w"))
            # Checking if positive
            if 'yes' in answer.lower() or 'yeah' in answer.lower():
                # Grabbing the next 100 lines to check them for follow-ups
                batch = t_lines[counter+2:min(counter+100, len(t_lines))]
                for index, line in enumerate(batch):
                    # Checking the line if it contains a question
                    f_in_t = any([f_line in line for f_line in f_lines])
                    if f_in_t:
                        # Printing Q and A
                        answer = batch[index+1]
                        print(f'{line}{sep}{answer}{sep}', end='', file=open("BANTP Results/p_result.txt", "a"))
                    elif counter % 2 == 0:
                        # No follow-up - move to the next question
                        # Except it's customer's reply
                        break




import csv

with open('Testing2File.csv') as file: 
    csvReader = csv.DictReader(file, lineterminator='\n', fieldnames=["Call Number", "P Audio Name", "P Response", "P Category"])
    for row in csvReader:
        CallNumber = (row['Call Number'])

if CallNumber == "Number":
    number = 1
else:
    number = int(CallNumber)
    number += 1

B_result=open('BANTP Results/b_result.txt')
B_lines=B_result.readlines()

A_result=open('BANTP Results/a_result.txt')
A_lines=A_result.readlines()

N_result=open('BANTP Results/n_result.txt')
N_lines=N_result.readlines()

T_result=open('BANTP Results/t_result.txt')
T_lines=T_result.readlines()

P_result=open('BANTP Results/p_result.txt')
P_lines=P_result.readlines()

B_Response = B_lines[2]
A_Response = A_lines[2]
N_Response = N_lines[2]
T_Response = T_lines[2]
P_Response = P_lines[2]

B_Question = B_lines[0]
A_Question = A_lines[0]
N_Question = N_lines[0]
T_Question = T_lines[0]
P_Question = P_lines[0]

#Budget if
if "yes" in B_Response.lower() or "yeah" in B_Response.lower() or "yep" in B_Response.lower() or "right" in B_Response.lower():
    B_Category = "Certain Yes"
elif "i think so" in B_Response.lower():
    B_Category = "Uncertain Yes"
elif "i'm not sure" in B_Response.lower():
    B_Category = "Uncertain No"
elif "no" in B_Response.lower():
    B_Category = "Certain No"
else:
    B_Category = "Other"

#Authority if
if "recommender" in A_Response.lower() or "recommends" in A_Response.lower() or "recommend" in A_Response.lower()  or "recommended" in A_Response.lower():
    A_Category = "Recommender"
elif "final decision maker" in A_Response.lower() or "final decision" in A_Response.lower():
    A_Category = "Final Decision Maker"
else:
    A_Category = "Other"

#Need if
if "yes" in N_Response.lower() or "yeah" in N_Response.lower() or "yep" in N_Response.lower() or "right" in N_Response.lower():
    N_Category = "Certain Yes"
elif "i think so" in N_Response.lower():
    N_Category = "Uncertain Yes"
elif "i'm not sure" in N_Response.lower():
    N_Category = "Uncertain No"
elif "no" in N_Response.lower():
    N_Category = "Certain No"
else:
    N_Category = "Other"

#Time if
if "couple of months" in T_Response.lower() or "six months" in T_Response.lower():
    T_Category = "6 Months"
elif "one year" in T_Response.lower():
    T_Category = "12 Months"
elif "two years" in T_Response.lower():
    T_Category = "24 Months"
elif "three years" in T_Response.lower():
    T_Category = "36 Months"
else:
    T_Category = "Other"

#Partner if
if "yes" in P_Response.lower() or "yeah" in P_Response.lower() or "yep" in P_Response.lower() or "right" in P_Response.lower() or "i spoke with" in P_Response.lower() or "i spoke to" in P_Response.lower():
    P_Category = "Certain Yes"
elif "i think so" in P_Response.lower():
    P_Category = "Uncertain Yes"
elif "i'm not sure" in P_Response.lower():
    P_Category = "Uncertain No"
elif "no" in P_Response.lower():
    P_Category = "Certain No"
else:
    P_Category = "Other"

with open('Testing2File.csv', 'a') as file:
    csvWriter = csv.writer(file, lineterminator='\n')

    csvWriter.writerow([number, random_file, "", B_Question, B_Response, B_Category, "", A_Question, A_Response, A_Category, "", N_Question, N_Response, N_Category, "", T_Question, T_Response, T_Category, "", P_Question, P_Response, P_Category])


# with open('Testing2File.csv', 'w') as file:
#   csvWriter = csv.writer(file, lineterminator='\n')

#   csvWriter.writerow(["", "", "", "Budget", "", "", "", "Authority", "", "", "", "Need", "", "", "", "Time", "", "", "", "Partner"])
#   csvWriter.writerow(["Number", "Audio File", "", "Question", "Response", "Category", "", "Question", "Response", "Category", "", "Question", "Response", "Category", "", "Question", "Response", "Category", "", "Question", "Response", "Category"])

1 个答案:

答案 0 :(得分:0)

代码中的问题可能是您两次打开了结果文件:在with块和print函数中:

with open(file) as t_file,\
open ('b_result.txt') as b_result_file,\

print(f'{line}{sep}{answer}{sep}', end='', file=open("b_result.txt", "a"))

尝试不要在with块中打开文件或将b_result_file传递给打印功能,而不是再次重新打开它,而要在其中使用修饰符a