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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bajajv
    New Member
    • Jun 2007
    • 152

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

    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 -
    Code:
    import libxml2
    import fileinput
    import csv
    import sys
    
    MsgFile = sys.argv[1]
    XPaths  = sys.argv[2]
    
    msg = open(MsgFile, 'rt')
    
    delim = ','
    quote = '"'
    values = ''
    
    ofile = open('results_.csv', "wb")
    writer = csv.writer(ofile, dialect='excel')
    for line in msg:
            values = '(['
            doc = libxml2.parseDoc(line)
            ctxt = doc.xpathNewContext()
            ctxt.xpathRegisterNs("t", "http://url.abcd.com/ABCD")
    
            xpaths = open(XPaths, 'rt')
            #values += quote
            for path in xpaths:
                    s1 = ctxt.xpathEval( path )
                    var = str(s1[0].content)
                    values += ('\'')
                    values += var
                    values += ('\'')
                    values += delim
    
            values = values[:-1]
            values += '])'
            print values
            writer.writerow(values)
            values = ''
    
            doc.freeDoc()
            ctxt.xpathFreeContext()
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    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.
    Code:
    line_ctr = 0
    for line in msg:
        if line_ctr < 20:
            print line
            line_ctr += 1
    You perhaps want readlines
    Code:
    msg = open(MsgFile, 'r').readlines()

    Comment

    Working...