xlrd 中打印的 col 输出似乎是 xf 格式文本.我该如何摆脱这个? [英] The col output in xlrd printing something with appears to be xf formatting text. How do I get rid of this?

查看:52
本文介绍了xlrd 中打印的 col 输出似乎是 xf 格式文本.我该如何摆脱这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XLRD 尝试读取和操作封装在我的 excel 文档单元格中的字符串文本.我正在发布我的代码,以及当我选择打印某个列时返回的文本.

I am using XLRD to attempt to read from and manipulate string text encapsulated within the cells of my excel document. I am posting my code, as well as the text that is returned when I choose to print a certain column.

import xlrd
data = xlrd.open_workbook('data.xls')
sheetname = data.sheet_names()
employees = data.sheet_by_index(0)

print employees.col(2)

>>>[text:u'employee_first', text:u'\u201cRichard\u201d', text:u'\u201cCatesby\u201d', text:u'\u201cBrian\u201d']

我的目的是创建一个 dict 或在 python 中使用字符串引用 excel 文档.我希望我的程序中有许多函数在本地操作数据,然后在稍后(不在此问题的范围内)输出到第二个 excel 文件.

My intention is to create a dict or either reference the excel documents using strings in python. I would like to have a number of my functions in my program manipulate the data locally and then output at a later point (not within the scope of this question) to a second excel file.

如何去除这些额外信息?

How do I get rid of this extra information?

推荐答案

employees.col(2)xlrd.sheet.Cell 实例的列表.要从列中获取所有值(而不是 Cell 对象),您可以使用 col_values 方法:

employees.col(2) is a list of xlrd.sheet.Cell instances. To get all the values from the column (instead of the Cell objects), you can use the col_values method:

values = employees.col_values(2)

你也可以这样做(我最初的建议):

You could also do this (my original suggestion):

values = [c.value for c in employees.col(2)]

但这比使用 col_values 效率低得多.

but that is much less efficient than using col_values.

\u201c\u201d 分别是 unicode 左右双引号.如果你想摆脱这些,你可以使用 lstrip 和 rstrip 字符串方法.例如.像这样:

\u201c and \u201d are unicode left and right double quotes, respectively. If you want to get rid of those, you can use, say, the lstrip and rstrip string methods. E.g. something like this:

values = [c.value.lstrip(u'\u201c').rstrip(u'\u201d') for c in employees.col(2)]

这篇关于xlrd 中打印的 col 输出似乎是 xf 格式文本.我该如何摆脱这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆