0

Hi, I was wondering if someone could help me wrap my head round iterations in a while loop. What I am trying to do is do a word replace and then add a numbering to each replace.

IE

Therapist bl;kja;lafkja;ldkjad;l

Patient ;alkjf;dljkad;ljkfl;kaj;

Therapist bl;kja;lafkja;ldkjad;l

Patient ;alkjf;dljkad;ljkfl;kaj;

To

T1 bl;kja;lafkja;ldkjad;l

P1 ;alkjf;dljkad;ljkfl;kaj;

T2 bl;kja;lafkja;ldkjad;l

P2 ;alkjf;dljkad;ljkfl;kaj;

What I have so far I have borrowed from ghostdog74 ( thank you so much for getting me started). The code below can change the word and iterate the current line but not the individual replaced name. IE Therapist to T1, Patient to P1, Therapist to T2 ect.

Thanks in advance.

Don

#! /usr/bin/env python2.7
import os, sys 

count = 0 



f = open("header_text.txt")
o = open("Target.txt","a")


name1 = raw_input("Please tell me who the first speaker is: ")


while 1:
    line = f.readline()
    if not line: break
    count = count + 1 
    nameCount = name1 + str(count)
    #print nameCount
    line = line.replace("Therapist", nameCount)

    o.write(line + "\n")
o.close()
2
Contributors
2
Replies
34
Views
2 Years
Discussion Span
Last Post by dcampbell
0

Hint:

>>> transform = dict((a.strip() for a in  pair.split('to'))
     for pair in 'Therapist to T, Patient to P'.split(','))
>>> counts = dict((a, 0) for a  in transform)
>>> counts['Therapist'] += 1
>>> print transform['Therapist'] + str(counts['Therapist'])
T1
>>> counts['Therapist'] += 1
>>> print transform['Therapist'] + str(counts['Therapist'])
T2

Edited by pyTony

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.