Converting xls to csv in Python 3 using xlrd

2024/11/19 4:25:45

I'm using Python 3.3 with xlrd and csv modules to convert an xls file to csv. This is my code:

import xlrd
import csvdef csv_from_excel():wb = xlrd.open_workbook('MySpreadsheet.xls')sh = wb.sheet_by_name('Sheet1')your_csv_file = open('test_output.csv', 'wb')wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)for rownum in range(sh.nrows):wr.writerow(sh.row_values(rownum))your_csv_file.close()

With that I am receiving this error: TypeError: 'str' does not support the buffer interface

I tried changing the encoding and replaced the line within the loop with this:

wr.writerow(bytes(sh.row_values(rownum),'UTF-8'))

But I get this error: TypeError: encoding or errors without a string argument

Anyone know what may be going wrong?

Answer

Try this

import xlrd
import csvdef csv_from_excel():wb = xlrd.open_workbook('MySpreadsheet.xlsx')sh = wb.sheet_by_name('Sheet1')your_csv_file = open('output.csv', 'w', encoding='utf8')wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)for rownum in range(sh.nrows):wr.writerow(sh.row_values(rownum))your_csv_file.close()
https://en.xdnf.cn/q/70980.html

Related Q&A

HTMLParser.HTMLParser().unescape() doesnt work

I would like to convert HTML entities back to its human readable format, e.g. £ to £, ° to etc.Ive read several posts regarding this question Converting html source content into read…

What security issues need to be addressed when working with Google App Engine?

Ive been considering using Google App Engine for a few hobby projects. While they wont be handling any sensitive data, Id still like to make them relatively secure for a number of reasons, like learnin…

Supporting multiple Python module versions (with the same version of Python)

I looked around but cannot find a clear answer to my question.I have a very legitimate need for supporting N-versions of the same Python module.If they are stored in the same same package/directory, th…

ImportError: cannot import name signals

Im using Django 1.3.0 with Python 2.7.1. In every test I write the following imports I get the importError above:from django.utils import unittest from django.test.client import ClientThe full stack tr…

Return a Pandas DataFrame as a data_table from a callback with Plotly Dash for Python

I would like to read a .csv file and return a groupby function as a callback to be displayed as a simple data table with "dash_table" library. @Lawliets helpful answer shows how to do that wi…

Nose: How to skip tests by default?

I am using Pythons nose and I have marked some of my tests as "slow", as explained in the attrib plugin documentation.I would like to skip all "slow" Tests by default when running n…

SQLAlchemy ORM select multiple entities from subquery

I need to query multiple entities, something like session.query(Entity1, Entity2), only from a subquery rather than directly from the tables. The docs have something about selecting one entity from a s…

How to ensure data is received between commands

Im using Paramiko to issue a number of commands and collect results for further analysis. Every once in a while the results from the first command are note fully returned in time and end up in the out…

Format Excel Column header for better visibility and Color

I have gone through many posts but did not found the exact way to do the below. Sorry for attaching screenshot(Just for better visibility) as well , I will write it also. Basically it looks like -Name…

Using multiple keywords in xattr via _kMDItemUserTags or kMDItemOMUserTags

While reorganizing my images, in anticipation of OSX Mavericks I am writing a script to insert tags into the xattr fields of my image files, so I can search them with Spotlight. (I am also editing the …