Parsing Problems

S

saif.shakeel

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
 
M

Marc 'BlackJack' Rintsch

saif.shakeel said:
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.

It's not that you store them in the list but that you simply print the
string representation of the list which does not meet your requirements.
You want to join the list elements with a comma::

print 'supported=%s' % ','.join(ser)

Ciao,
Marc 'BlackJack' Rintsch
 
D

Daniel Nogradi

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.


mylist = ['0x3e', '0x28', '0x3b', '0x22', '0x20', '0x27', '0xaa',
'0x10', '0xae', '0x34', '0x36', '0x2d', '0xa9', '0xa5', '0x4', '0xa2',
'0x1a']

','.join( mylist )

This will give you:

0x3e,0x28,0x3b,0x22,0x20,0x27,0xaa,0x10,0xae,0x34,0x36,0x2d,0xa9,0xa5,0x4,0xa2,0x1a


Daniel
 
I

irstas

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
 
B

bytecolor

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
l = ['0x3e', '0x28', '0x3b', '0x22', '0x20', '0x27', '0xaa']
','.join(l)
'0x3e,0x28,0x3b,0x22,0x20,0x27,0xaa'

You may want to look at the ConfigParser package too. It's for reading/
writing .ini file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top