473,378 Members | 1,152 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Parsing Problems

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

Apr 3 '07 #1
4 1146
In <11**********************@q75g2000hsh.googlegroups .com>, saif.shakeel
wrote:
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
Apr 3 '07 #2

sa**********@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

Apr 3 '07 #3
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
Apr 3 '07 #4
On Apr 3, 5:13 am, saif.shak...@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
>>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.

--
bytecolor

Apr 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
19
by: Alex Mizrahi | last post by:
Hello, All! i have 3mb long XML document with about 150000 lines (i think it has about 200000 elements there) which i want to parse to DOM to work with. first i thought there will be no...
3
by: Girish | last post by:
Hi All, I have written a component(ATL COM) that wraps Xerces C++ parser. I am firing necessary events for each of the notifications that I have handled for the Content and Error handler. The...
3
by: Eric Lilja | last post by:
Hello, I'm creating a small utility for an online game. It involves parsing a text file of "tradesskill recipes" and inserting these recipes in a gui tree widget (similar to gui file browsers if...
4
by: Hugh | last post by:
Hello, I am having some problems understanding (most likely), parsing a text file. I would like to parse a file like: block1 { stuff; ... stuffN; };
3
by: Pir8 | last post by:
I have a complex xml file, which contains stories within a magazine. The structure of the xml file is as follows: <?xml version="1.0" encoding="ISO-8859-1" ?> <magazine> <story>...
13
by: charliefortune | last post by:
I am fetching some product feeds with PHP like this $merch = substr($key,1); $feed = file_get_contents($_POST); $fp = fopen("./feeds/feed".$merch.".txt","w+"); fwrite ($fp,$feed); fclose...
3
by: xahlee | last post by:
In the past weeks i've been thinking over the problem on the practical problems of regex in its matching power. For example, often it can't be used to match anything of nested nature, even the most...
1
by: Rick Owen | last post by:
Greetings, I have a form that, when submitted, calls a plsql procedure. The form has a number of fields (text, hidden, select, radio) but the particular field that is giving me problems is a...
2
by: Felipe De Bene | last post by:
I'm having problems parsing an HTML file with the following syntax : <TABLE cellspacing=0 cellpadding=0 ALIGN=CENTER BORDER=1 width='100%'> <TH BGCOLOR='#c0c0c0' Width='3%'>User ID</TH> <TH...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.