By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,106 Members | 2,679 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,106 IT Pros & Developers. It's quick & easy.

How to remove the carriage returns from XML data.

P: 8
Hi,

I am having a xml code like this

<name>
I am having a carriage return here.
Second line with carriage return.
This is the last line.
</name>

I am using xml.parsers.expat for parsing the above mentioned xml

Is there any way to strip of all the carriage returns and get the data as

I am having a carriage return here. Second line with carriage return. This is the last line.
Jan 11 '07 #1
Share this Question
Share on Google+
6 Replies


bartonc
Expert 5K+
P: 6,596
I don't have xmlparser experience, but python can strip newlines like this:

>>> anStr = "I am having a carriage return here.\n"
>>> print anStr
I am having a carriage return here.

>>> print anStr.strip("\n")
I am having a carriage return here.
>>>
Jan 11 '07 #2

P: 8
Thanks,

but unfortunately this data is not explicitly provided with '\n' . Some other form of carriage return is present which I am not able to strip of. I have checked the data in text pad also , there is no explicit '\n' given :-(
Jan 11 '07 #3

dshimer
Expert 100+
P: 136
I would be really interested in seeing a snippet of the code that shows the carriage control character showing up. How is it being read, joined, and output?

Thanks,

but unfortunately this data is not explicitly provided with '\n' . Some other form of carriage return is present which I am not able to strip of. I have checked the data in text pad also , there is no explicit '\n' given :-(
Jan 11 '07 #4

Expert 100+
P: 511
i am sure there's a better way, but here's what i have
Expand|Select|Wrap|Line Numbers
  1. flag =0
  2. for line in open("file"):
  3.         if "<name>" in line:
  4.                 flag = 1
  5.                 print line
  6.         elif "</name>" in line:
  7.                 print "\n" + line
  8.                 flag = 0
  9.         elif flag:
  10.                 print line.strip(),
  11.  
Jan 12 '07 #5

bvdet
Expert Mod 2.5K+
P: 2,851
In my limited experience with Python module xml.sax, each line will be displayed on its own line for output. You can strip the linefeed character with escape(), but each line will display on separate lines. You could process the XML file, process the output from the parser, or write your own parser. This script will read an XML file, strip the linefeed characters on lines from '<name>' to '</name>' and output to another file:
Expand|Select|Wrap|Line Numbers
  1. inFile = open('C:\\YourDirectory\\Test.xml', 'r')
  2. lineList = inFile.readlines()
  3. inFile.close()
  4. outFile = open('C:\\YourDirectory\\Test2.xml', 'w')
  5.  
  6. lastIndexS, lastIndexE = 0, 0
  7. for item in lineList:
  8.     if '<name>' in item:
  9.         indexStart = lineList.index(item, lastIndexS+1)
  10.         lastIndexS = indexStart
  11.  
  12.     if '</name>' in item:
  13.         indexEnd = lineList.index(item, lastIndexE+1)
  14.         lastIndexE = indexEnd
  15.         for i in range(indexStart, indexEnd):
  16.             if lineList[i] == '<name>\n':
  17.                 lineList[i] = lineList[i].strip('\n')
  18.             else: lineList[i] = lineList[i].strip('\n')+" "
  19.  
  20. outFile.writelines(lineList)
  21. outFile.close()
Here is a before and after:
Expand|Select|Wrap|Line Numbers
  1. <name>I am having a carriage return here.
  2. Second line with carriage return.
  3. This is the last line.
  4. </name>
  5. <name>I am having a carriage return here. Second line with carriage return. This is the last line.</name>
  6. <name>
  7. I am having a carriage return here.
  8. Second line with carriage return.
  9. This is the last line.
  10. </name>
  11. <name>I am having a carriage return here.
  12. Second line with carriage return.
  13. This is the last line.
  14. </name>
  15.  
  16.  
  17. <name>I am having a carriage return here. Second line with carriage return. This is the last line. </name>
  18. <name>I am having a carriage return here. Second line with carriage return. This is the last line.</name>
  19. <name>I am having a carriage return here. Second line with carriage return. This is the last line. </name>
  20. <name>I am having a carriage return here. Second line with carriage return. This is the last line. </name>
  21.  
xml.sax output:
Expand|Select|Wrap|Line Numbers
  1. Start:  name
  2. I am having a carriage return here. Second line with carriage return. This is the last line. 
  3. End:  name
  4.  
  5. Start:  name
  6. I am having a carriage return here. Second line with carriage return. This is the last line.
  7. End:  name
  8.  
  9. Start:  name
  10. I am having a carriage return here. Second line with carriage return. This is the last line. 
  11. End:  name
  12.  
  13. Start:  name
  14. I am having a carriage return here. Second line with carriage return. This is the last line. 
  15. End:  name
HTH
Jan 12 '07 #6

P: 1
<xsl:text>&#32</xsl:text> at the end of the first section of code where the
<xsl:value-of select="fieldname"

<newfield><xsl:value-of select="fieldname"/><xsl:text>&#32</xsl:text><xsl:value-of select="fieldname2"/></newfield>
May 19 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.