| | |
Loop string text replacement
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2005
Posts: 3
Reputation:
Solved Threads: 0
Hi all,
I have the following string:
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?
I have the following string:
Python Syntax (Toggle Plain Text)
<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>
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?
•
•
Join Date: Jun 2008
Posts: 122
Reputation:
Solved Threads: 30
A pragmatic one time solution:
The test.txt contains the above string.
The test.txt contains the above string.
python Syntax (Toggle Plain Text)
st=open("test.txt").read() delimiter="><" tokens=st.split(delimiter) fo=open("test_out.txt","w") count=1 for line in tokens: if line.startswith("ARG") and line.endswith("ARG"): firstarg=line.find("ARG")+3 line=line[:firstarg]+str(count)+line[firstarg:]+str(count) count+=1 fo.write(line) fo.write(delimiter)#FIXME last delimiter is too much fo.close()
Last edited by slate; May 26th, 2009 at 3:19 pm.
![]() |
Similar Threads
- find and replace string in a text file (C)
- Printing   for formatting String text (Java)
- Search string in a text file (C)
- using a "for" loop to read a text file (VB.NET)
- Read in a string from a text file (C)
- New User (C)
Other Threads in the Python Forum
- Previous Thread: Python Package Error
- Next Thread: Decision making python code
| Thread Tools | Search this Thread |
alarm assignment avogadro beginner bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator getvalue gnu graphics gui halp homework http ideas import input ip itunes java leftmouse line linux list lists logging loop maintain maze millimeter module mouse number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tlapse tuple tutorial ubuntu unicode urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia wxpython xlib






