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()

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

Thank you! Let me try that!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.