Parsing Problems

irstas at gmail.com irstas at gmail.com
Tue Apr 3 05:34:23 EDT 2007


saif.shakeel at gmail.com wrote:
> Hi,
>
>      I have just started learning python.I need to parse an XML file
> and present the contents in a particular format.The format is called
> as "ini" file.I have written some code.A section of the format needs
> the data to be present in format as given below:
>
> [Services]
> supported=0x10,0x1A,0x3B,0x20,0x27,0x28,0x34,0x36,0x3E,0xA2,0xA5,0x2D,
> 0x22,0xA9,0x04,0xAA,0xAE
>
> My code for this section parses the xml file and gives :
> [Services]
> Supported=['0x3e', '0x28', '0x3b', '0x22', '0x20', '0x27', '0xaa',
> '0x10', '0xae', '0x34', '0x36', '0x2d', '0xa9', '0xa5', '0x4', '0xa2',
> '0x1a']
>
>                 {forget the numericals matching}.As you can see there
> are single quotes around numericals ,which is not desired .I think the
> problem lies in me using a list for storing and later printing out
> values.I have attached few lines of code,not sure how clear it can be
> to you:
>
>                         for l in range(0,len(ser_par),
> 1):
>                             if
> ser_par[l].getAttribute('Semantics')==u'serviceId':
>                                 if
> tag_exists(ser_par[l].childNodes,'VALUE'):
>                                     val =
> ser_par[l].getElementsByTagName('VALUE')
>                                     value =
> str(val[0].getAttribute('value'))
>                                     valu = hex(int(value))
>                                     rval.append(valu)
>                                     ser_par_num = ser_par_num + 1
>                     prim_num = prim_num + 1
>
>                 service_num = service_num + 1
>         variant_num = variant_num + 1
>     ser = list(set(rval))
>
> print "\n[Services]"
>     print "Supported="+str(ser)
>
>               How can i get rid of those single quotes.
>
>                                                      Thanking you
>
> shakeel

Hi,
Instead of using str(ser), you should create a string which combines
the individual strings in ser, separated by commas. Luckily for you,
there's a string method does just that: join.
http://docs.python.org/lib/string-methods.html




More information about the Python-list mailing list