[Tutor] Python doubt

Walter Prins wprins at gmail.com
Tue Aug 21 14:49:02 CEST 2012


Hello Akarsh,

Welcome to the Python tutor list.

On 21 August 2012 13:29, akarsh a <akarsh.iirs at gmail.com> wrote:
> sir,
>      How to read an excel file row by row and write each row to different
> text files?
> ie, if i have an excel file consists of m raw and n columns as follws
> 1  as  mmm   15    25   cv   10.2
> 2  xv  fvbgd   14    11   dc   12.5
> ..................................................
> I want to read each line and write it to separate text file ie, a.txt,
> b.txt,..............m
> is there any python code for this purpose . please explain me the python
> modules required to serve this purpose and code because i am new to python
> ......

1) You can read .xls files using the "xlrd" module.  It is not
included by default with Python but you can install it easily enough
on your Python installation.  The preferred method to do this will
vary by operating system.  What operating system are you using?  You
can find out more about the xlrd module here:
http://pypi.python.org/pypi/xlrd/ Note there's a parallel module for
writing named "xlwt".

2) You can write text files using the simple file I/O functions e.g.

newLine = 'blah blah blah'
file = open("datafile.txt", "w")
file.write(newLine)
file.close()

etc.

You may also want to look at the "csv" module, for example:
import csv
somedata = ["value %d" % i for i in range(1,4)]
fileobj = open("datafile.csv","w")
csvwriter = csv.writer(fileobj, delimiter=',',quoting=csv.QUOTE_ALL)
csvwriter.writerow(somedata)


HTH,

Walter


More information about the Tutor mailing list