简体   繁体   中英

Python code running in steps rather than as a whole

I have a bit of code that will read a transcript and search for a list of phrases which are stored in a different text file. If a match is found, it prints the matching line followed by the next line (response), this is stored in a result text file. This result text file is then read again, and put into an excel file in its designated area.

I have the code working to the point where it generates the results and puts them in the excel spreadsheet where it needs to go, however. The data in the results are not updating. I have the code randomly choosing a transcript out of a list of 10 to scatter the results, however I have to run the entire code over and over again to get all 5 category results updated . The five categories are:

B / A / N / T / P

Does anyone know where I am going wrong here? Or even how to get all of the analysis sections to run at the same time rather than one by one?

My code is as follows:

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"])

The problem in your code may probably be that you open result file twice: in with block and in print function:

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

and

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

try to not open the file in with block or pass b_result_file into the print function instead of reopening it again, but use a modifier a in it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM