Loop string text replacement

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2005
Posts: 3
Reputation: adamdidthis is an unknown quantity at this point 
Solved Threads: 0
adamdidthis adamdidthis is offline Offline
Newbie Poster

Loop string text replacement

 
0
  #1
May 25th, 2009
Hi all,

I have the following string:

  1. <REQUEST><KEY_HEADER><STATUS>CREATE</STATUS><TIME_CODE>0</TIME_CODE><COUNT>344</COUNT><TARGET>8</TARGET></KEY_HEADER><KEY_INFO><NAME>TEAMLIST</NAME><ARGS>97</ARGS><ARG>COACH: XXX</ARG><ARG>Crusaders</ARG><ARG>WHITE</ARG><ARG>BLACK</ARG><ARG>Crusaders</ARG><ARG>WYATT</ARG><ARG>CROCKETT</ARG><ARG>JASON</ARG><ARG>MacDONALD</ARG><ARG>OWEN</ARG><ARG>FRANKS</ARG><ARG>BRAD</ARG><ARG>THORN</ARG><ARG>ISAAC</ARG><ARG>ROSS</ARG><ARG>KIERAN</ARG><ARG>READ</ARG><ARG>RICHIE</ARG><ARG>McCAW</ARG><ARG>THOMAS</ARG><ARG>WALDROM</ARG><ARG>ANDY</ARG><ARG>ELLIS</ARG><ARG>STEPHEN</ARG><ARG>BRETT</ARG><ARG>ADAM</ARG><ARG>WHITELOCK</ARG><ARG>RYAN</ARG><ARG>CROTTY</ARG><ARG>TIM</ARG><ARG>BATEMAN</ARG><ARG>JARED</ARG><ARG>PAYNE</ARG><ARG>LEON</ARG><ARG>MacDONALD</ARG><ARG>DANIEL</ARG><ARG>PERRIN</ARG><ARG>BRONSON</ARG><ARG>MURRAY</ARG><ARG>MICHAEL</ARG><ARG>PATERSON</ARG><ARG>JONATHON</ARG><ARG>POFF</ARG><ARG>KAHN</ARG><ARG>FOTUALI'I</ARG><ARG>SEAN</ARG><ARG>MAITLAND</ARG><ARG>HAMISH</ARG><ARG>GARD</ARG><ARG> </ARG><ARG> </ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG></ARG><ARG> </ARG><ARG>1</ARG><ARG>2</ARG><ARG>3</ARG><ARG>4</ARG><ARG>5</ARG><ARG>6</ARG><ARG>7</ARG><ARG>8</ARG><ARG>9</ARG><ARG>10</ARG><ARG>11</ARG><ARG>12</ARG><ARG>13</ARG><ARG>14</ARG><ARG>15</ARG><ARG>16</ARG><ARG>17</ARG><ARG>18</ARG><ARG>19</ARG><ARG>20</ARG><ARG>21</ARG><ARG>22</ARG><ARG> </ARG></KEY_INFO></REQUEST>
  2.  

Yep I know it's a mess but it's the only way I can receive it.

What I need to do is loop through the string and change ARG to be ARG then a number e.g.

<ARG>14</ARG> changes to <ARG1>14</ARG1>

Does this make sense and is it even possible?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,025
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Loop string text replacement

 
0
  #2
May 25th, 2009
Python would make this easily possible, but you need to tell us if ARG always turns into ARG1, or is the next ARG a ARG2 and so on?
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 3
Reputation: adamdidthis is an unknown quantity at this point 
Solved Threads: 0
adamdidthis adamdidthis is offline Offline
Newbie Poster

Re: Loop string text replacement

 
0
  #3
May 25th, 2009
Originally Posted by vegaseat View Post
Python would make this easily possible, but you need to tell us if ARG always turns into ARG1, or is the next ARG a ARG2 and so on?
Yep I should end up with ARG1, ARG2 etc, sorry I should have made that clearer
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 122
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: Loop string text replacement

 
0
  #4
May 26th, 2009
A pragmatic one time solution:
The test.txt contains the above string.
  1. st=open("test.txt").read()
  2. delimiter="><"
  3. tokens=st.split(delimiter)
  4. fo=open("test_out.txt","w")
  5. count=1
  6. for line in tokens:
  7. if line.startswith("ARG") and line.endswith("ARG"):
  8. firstarg=line.find("ARG")+3
  9. line=line[:firstarg]+str(count)+line[firstarg:]+str(count)
  10. count+=1
  11. fo.write(line)
  12. fo.write(delimiter)#FIXME last delimiter is too much
  13. fo.close()
Last edited by slate; May 26th, 2009 at 3:19 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC