xlrd

Python: xlrd discerning dates from floats

六月ゝ 毕业季﹏ 提交于 2019-12-04 03:21:32
I wanted to import a file containing text, numbers and dates using xlrd on Python. I tried something like: if "/" in worksheet.cell_value: do_this else: do_that But that was of no use as I latter discovered dates are stored as floats, not strings. To convert them to datetime type I did: try: get_row = str(datetime.datetime(*xlrd.xldate_as_tuple(worksheet.cell_value(i, col - 1), workbook.datemode))) except: get_row = unicode(worksheet.cell_value(i, col - 1)) I have an exception in place for when the cell contains text. Now i want to get the numbers as numbers and the dates as dates, because

Reading a cell value that contains a formula returns 0.0 when using xlrd

一笑奈何 提交于 2019-12-04 00:31:35
I try to read a cell value, say E5 and E5 in the excel sheet contains a formula ' =(A29 - A2) '. I use the following code and it returns me 0.00 instead of the actual value 1.440408 . Is there a way to solve this? I want to print the correct value. Please help me with this. Thank you. book = xlrd.open_workbook('Test.xlsx') first_sheet = book.sheet_by_index(0) particular_cell_value = (first_sheet.cell_value(4,4)) print(particular_cell_value) Excel files store formulas and values independently. If the files are saved directly by Excel, then Excel will write both. However, files generated by

How to set NULL for IntegerField instead of setting 0?

独自空忆成欢 提交于 2019-12-03 23:19:39
I'm uploading some data from an excel file using xlrd and turning that data into models (with mainly IntegerField values) in Django. My excel file has a bunch of missing data. Unfortunately, these missing data are converted to the value of 0 in my models, instead of NULL. This means that when my Person model doesn't have an age, that age gets recorded as 0, instead of as NULL. What's the best way to change this? I've tried doing this: age = models.IntegerField(blank=True, null=True) But the blank fields still get set to 0 by default. Shwetabh Sharan Instead of using age = models.IntegerField()

Reading numeric Excel data as text using xlrd in Python

孤人 提交于 2019-12-03 15:33:09
问题 I am trying to read in an Excel file using xlrd, and I am wondering if there is a way to ignore the cell formatting used in Excel file, and just import all data as text? Here is the code I am using for far: import xlrd xls_file = 'xltest.xls' xls_workbook = xlrd.open_workbook(xls_file) xls_sheet = xls_workbook.sheet_by_index(0) raw_data = [['']*xls_sheet.ncols for _ in range(xls_sheet.nrows)] raw_str = '' feild_delim = ',' text_delim = '"' for rnum in range(xls_sheet.nrows): for cnum in range

How to check if valid excel file in python xlrd library

ぐ巨炮叔叔 提交于 2019-12-03 14:44:01
Is there any way with xlrd library to check if the file you use is a valid excel file? I know there's other libraries to check headers of files and I could use file extension check. But for the sake of multiplatformness I wonder if there's any function I could use in the xlrd library itself that could just return something like false when trying to open the file and then notify the user. I'm kind of new on Python so I tried getting something debugging the xlrd.open_workbook function with no success. You can try to open workbook but in try/except block to catch XLRDError exception in case if

利用python包(xlrd和xlwt)处理excel

你说的曾经没有我的故事 提交于 2019-12-03 14:34:10
一 读取excel 这里介绍一个不错的包xlrd ,可以工作在任何平台。这也就意味着你可以在Linux下读取Excel文件。 下载 http://pypi.python.org/pypi/xlrd 首先,打开workbook; import xlrd wb = xlrd.open_workbook('myworkbook.xls') 检查表单名字: wb.sheet_names() 得到第一张表单,两种方式:索引和名字 sh = wb.sheet_by_index(0) sh = wb.sheet_by_name(u'Sheet1') 递归打印出每行的信息: for rownum in range(sh.nrows): print sh.row_values(rownum) 如果只想返回第一列数据: first_column = sh.col_values(0) 通过索引读取数据: cell_A1 = sh.cell(0,0).value cell_C4 = sh.cell(rowx=3,colx=2).value 注意:这里的索引都是从0开始的。 问题: 1. 解析日期2005-7-8这样的单元格后,print 出来的是整数:38541 2. 解析日期12:35:00这样的单元格,print出来时浮点数:0.524305555556 处理日期和时间就卡住了,转换起来也很麻烦

Read time from excel sheet using xlrd, in time format and not in float

不问归期 提交于 2019-12-03 12:35:31
I am trying to read some data from a excel file. One of the columns has time values in the format HH:MM:SS. Xlrd reads this time and converts it into float. I have another time values in my python file which I want to compare with the excel-imported time values. I am not able to do that as long as one of them is a "time" and the other is a "float". Any suggestions? This is how my excel file looks like - Time L_6_1 PW_6_1 Tc_6_1 Te_6_1 0:00:00 10000 500 290 270 1:00:00 10000 600 290 270 2:00:00 10000 700 290 270 3:00:00 10000 800 290 270 4:00:00 10000 900 290 270 And this is how I am reading

python xlrd unsupported format, or corrupt file.

匿名 (未验证) 提交于 2019-12-03 09:05:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: My code: import xlrd wb = xlrd.open_workbook("Z:\\Data\\Locates\\3.8 locates.xls") sh = wb.sheet_by_index(0) print sh.cell(0,0).value The error: Traceback (most recent call last): File "Z:\Wilson\tradedStockStatus.py", line 18, in wb = xlrd.open_workbook("Z:\\Data\\Locates\\3.8 locates.xls") File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 429, in open_workbook biff_version = bk.getbof(XL_WORKBOOK_GLOBALS) File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 1545, in getbof bof_error('Expected BOF record; found %r' % self

How do you read excel files with xlrd on Appengine

喜你入骨 提交于 2019-12-03 05:09:43
I am using xlrd in appengine. I use flask I cant read the input file and it keeps on showing the same error message the code is def read_rows(inputfile): rows = [] wb = xlrd.open_workbook(inputfile) sh = wb.sheet_by_index(0) for rownum in range(sh.nrows): rows.append(sh.row_values(rownum)) return rows @app.route('/process_input/',methods=['POST','GET']) def process_input(): inputfile = request.files['file'] rows=read_rows(request.files['file']) payload = json.dumps(dict(rows=rows)) return payload I realize that this might be caused by not uploading and saving it as a file. Any workaround on

python编程之赋值和拷贝的区别概述及操作excel数据库(图)

£可爱£侵袭症+ 提交于 2019-12-03 04:55:33
python编程之赋值和拷贝的区别概述及操作excel数据库(图) 一、赋值 在Python中,对象的赋值就是简单的对象引用,这点和C++不同,如下所示: a = [1,2,”hello”,[‘python’, ‘C++’]] b = a 在上述情况下,a和b是一样的,他们指向同一片内存,b不过是a的别名,是引用。 我们可以使用bisa 去判断,返回True,表明他们地址相同,内容相同,也可以使用id()函数来查看两个列表的地址是否相同。 赋值操作(包括对象作为参数、返回值)不会开辟新的内存空间,它只是复制了对象的引用。也就是说除了b这个名字之外,没有其他的内存开销。修改了a,也就影响了b,同理,修改了b,也就影响了a。 二、浅拷贝(shallow copy) 浅拷贝会创建新对象,其内容非原对象本身的引用,而是原对象内第一层对象的引用。 浅拷贝有三种形式:切片操作、工厂函数、copy模块中的copy函数。 比如上述的列表a; 切片操作:b = a[ : ] 或者 b = [ x for x in a ]; 工厂函数:b = list( a ); copy函数:b = copy.copy( a ); 浅拷贝产生的列表b不再是列表a了,使用is判断可以发现他们不是同一个对象,使用id查看,他们也不指向同一片内存空间。但是当我们使用id(x) for x in a 和 id(x) for