473,468 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

bajajv
152 New Member
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
1 1193
dwblas
626 Recognized Expert Contributor
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

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

Similar topics

0
by: Jack | last post by:
Hi, I got a code that is supposed to build first column as blank and then columns corresponding to the query. However, the final result is coming with one extra column where the vendor code values...
0
by: Andrew | last post by:
With command-line interface ( 3.23.37, UNIX Socket ) all is well with column aliasing. However, column aliases disappear in Excel, over ODBC, when there are multiple (joined) tables in the query. ...
5
by: Hardy Wang | last post by:
Hi all, I am just wandering, is it possible to programmatically detect each column's data type of SalDataReader, DataSet.Tables, DataTable? Thanks for any suggestion! -- WWW:...
1
seshu
by: seshu | last post by:
Hi everybody this is seshu long back i remember me posting a code in vb how write a for each loop and getting the names of ech file in a perticular folder but today i want a...
1
watertraveller
by: watertraveller | last post by:
Hi all. My ultimate goal is to return two columns, where no single value appears anywhere twice. This means that not only do I want to check that nothing from column A appears in column B and...
1
akashazad
by: akashazad | last post by:
Hi All Pl some body tell me is it possible to have a datagridView in which each column can have diff no of rows in it?
20
by: PokerRebel | last post by:
I am new to MS Access and was wondering how do I code the following using vba and looping each column in the table. I have a table with 6 columns with about 500 records in the table. Col1...
1
by: ritesh247 | last post by:
I was wondering if anyone could help me out with this: lets say u have three arrays: A= B= C= I want to use each column from each array to get an output, such that when the code runs it...
7
by: CCGG26 | last post by:
...hey guys.. how can you write aprogram that prints the number of A's in each column of a multiple sequence alignment. For example for the multiple alignment below >human ACCT >mouse ACCT...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.