1

I need to convert excel to csv. But i can't multi write the csv file

import sys,os

sys.path.insert(0,'D:/apera/Python27/xlrd-0.9.3')

import xlrd
import csv

path = "D:/apera/Workspace/Sounding"

CSVFile1 = "D:/apera/Workspace/Sounding/sounding001.csv"
CSVFile2 = "D:/apera/Workspace/Sounding/sounding002.csv"


for root,dirs,files in os.walk(path):
    xlsfiles=[ _ for _ in files if _.endswith('.xls') ]
    for xlsfile in xlsfiles:
        wb = xlrd.open_workbook(os.path.join(root,xlsfile))
        n = len(wb.sheets())
        ws = wb.sheet_by_name("INPUT")

        with open(CSVFile1, 'wb') as csvfile:
            wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL, delimiter=';')
            for rownum in xrange(ws.nrows):
                wr.writerow(list(x.encode('latin1')for x in ws.row_values(rownum)))

        with open(CSVFile2, 'wb') as csvfile:
            wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL, delimiter=';')
            for rownum in xrange(ws.nrows):
                wr.writerow(list(x.encode('latin1')for x in ws.row_values(rownum)))

        csvfile.close()

This the edited question. I need to write the csv files. This is only 2 files that needs to write. So I write it two times. So is there a way to make it simpler. So I don't need to with open csvfile3, 4, 5 and so on.. Thanks

3
  • use pandas... read in as pandas.ExcelFile object, and then use DataFrame.to_csv() function to write to csv. Much easier I'd say. Commented Dec 15, 2014 at 7:22
  • is it pandas a library? Commented Dec 15, 2014 at 7:25
  • Yes it is a python lubray Commented Dec 17, 2014 at 2:50

2 Answers 2

2

If your question is about how to open multiple files using the "*" wildcard, then I think you could use the glob module (as you have already mentioned):

import glob


file_names = glob.glob("*.txt")

for file_name in file_names:
    f = open(file_name, 'wb')

    f.write("AAA")

And similar question regarding the filename wildcards in python was already discussed here.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do something like this (for writing to multiple csv files):

import sys,os,xlrd,csv

sys.path.insert(0,'D:/apera/Python27/xlrd-0.9.3')

path = "D:/apera/Workspace/Sounding"

files_to_write = ["D:/apera/Workspace/Sounding/sounding1.csv","D:/apera/Workspace/Sounding/sounding2.csv"] 

for root,dirs,files in os.walk(path):
    xlsfiles=[ _ for _ in files if _.endswith('.xls') ]
    for xlsfile in xlsfiles:
        wb = xlrd.open_workbook(os.path.join(root,xlsfile))
        n = len(wb.sheets())
        ws = wb.sheet_by_name("INPUT")
        for z in files_to_write:    
            with open(z, 'wb') as csvfile:
                wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL, delimiter=';')
                for rownum in xrange(ws.nrows):
                    wr.writerow(list(x.encode('latin1')for x in ws.row_values(rownum)))    

6 Comments

Indention with one space? That's unusual.
I don't mean to get all csv files. In my folder all of my files is in .xls. And i need to print all of them to csv files. So in the code have open(csvfile, 'wb'). It must open first then he can write it. So is there a way to multi open and write it?
Please check the edited answer. Is this what you want?
not only that. The most important is csvfile = open(CSVFile, 'wb')
Can you please be a bit more specific as to what csv files do you want to write? Do you have a specific pattern? I tried to answer as per what I understood.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.