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

how to write in excel wordwise in each column, currently getting result characterwise

bajajv
100+
P: 152
hi
I am trying to read an xml and trying to write in excel using csv.writer. I am providing xml file name and xpath file name at command line.
But getting output like this-
(,[,',1,0,0,6,T,0,0,0,5,6,5,4,3,0,',",",',2,0,1,3,-,0,1,-,2,3,',],)
While I want it like this-
1006T000565430,2013-01-23
Why am I getting comma after every character?
This is my code -
Expand|Select|Wrap|Line Numbers
  1. import libxml2
  2. import fileinput
  3. import csv
  4. import sys
  5.  
  6. MsgFile = sys.argv[1]
  7. XPaths  = sys.argv[2]
  8.  
  9. msg = open(MsgFile, 'rt')
  10.  
  11. delim = ','
  12. quote = '"'
  13. values = ''
  14.  
  15. ofile = open('results_.csv', "wb")
  16. writer = csv.writer(ofile, dialect='excel')
  17. for line in msg:
  18.         values = '(['
  19.         doc = libxml2.parseDoc(line)
  20.         ctxt = doc.xpathNewContext()
  21.         ctxt.xpathRegisterNs("t", "http://url.abcd.com/ABCD")
  22.  
  23.         xpaths = open(XPaths, 'rt')
  24.         #values += quote
  25.         for path in xpaths:
  26.                 s1 = ctxt.xpathEval( path )
  27.                 var = str(s1[0].content)
  28.                 values += ('\'')
  29.                 values += var
  30.                 values += ('\'')
  31.                 values += delim
  32.  
  33.         values = values[:-1]
  34.         values += '])'
  35.         print values
  36.         writer.writerow(values)
  37.         values = ''
  38.  
  39.         doc.freeDoc()
  40.         ctxt.xpathFreeContext()
  41.  
  42.  
Dec 6 '13 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 626
We have no way to tell what the variable "line" contains from the information given so I would suggest that you print the first 20 or so to see what it contains.
Expand|Select|Wrap|Line Numbers
  1. line_ctr = 0
  2. for line in msg:
  3.     if line_ctr < 20:
  4.         print line
  5.         line_ctr += 1 
You perhaps want readlines
Expand|Select|Wrap|Line Numbers
  1. msg = open(MsgFile, 'r').readlines() 
Dec 6 '13 #2

Post your reply

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