如何在Python中读取Excel文件? [英] How do I read Excel file in Python?
问题描述
如何用Python读取Excel文件?
我找到了一个包读取excel文件,可以在任何
平台上使用。
http://www.lexicon.net /sjmachin/xlrd.htm
我安装并处理了这些示例,我发现它以不同的方式打印了单元格的
内容。 br>
>> import xlrd
book = xlrd.open_workbook(" Calculation_file .xls")
book = xlrd.open_workbook(" testbook.xls")
sh = book.sheet_by_index(0)
for range in range(sh.nrows):
print sh.row(rx)
[text:u''name'',text:u' '地址'',文字:u''ph'']
[text:u''sudhir'',text:u''bangalore'',number:1234.0]
[text:你维奈,文本:u''bangalore,编号:3264.0]
我与切片有点困惑。帮助我....
谢谢,
问候,
Sudhir。
kath写道:
如何在Python中读取Excel文件?
我找到了一个读取excel文件的包,可以在任何
平台上使用。
嗨Sudhir,
到目前为止,非常好:-)
>
http://www.lexicon.net/sjmachin /xlrd.htm
我安装并处理了这些示例,我发现它以不同的方式打印了单元格的
内容。
> import xlrd
book = xlrd.open_workbook(" Calculation_file.xls")
book = xlrd。 open_workbook(" testbook.xls")
sh = book.sheet_by_index(0)
for range in range(sh.nrows):
print sh.row(rx)
[text:u''name'',text:u''address'',text:u''ph'']
[text:u''sudhir'',text:u''bangalore'',number:1234.0]
[text:u''vinay'',text:u ''bangalore'',号码:3264.0]
如果您在屏幕上复制/粘贴
时会有问题吗?
在这种情况下
打印sh.row(rx)
会出错;你必须输入
来表示范围内的rx .....
一行返回一系列Cell对象。你所看到的是
Python自动在行中的每个单元格上执行repr(单元格)。
Cell .__ repr__方法将其格式化为调试方式。以下是我的一个小测试文件中的一些
示例:
>>导入xlrd
bk = xlrd.open_workbook('sjm1.xls'')
sh = bk.sheet_by_index(0)
row0 = sh.row(0)
row0
[text:u''fubar'',number:1.0,number:2.0]
>> firstcell = row0 [0]
type(firstcell)
< class''xlrd.sheet.Cell''>
>> firstcell.ctype
1
>> #cell type 1是text
firstcell.value
u''fubar''
>> repr(firstcell)
" text:u''fubar''"
>
我有点困惑切片。帮助我....
上述所有内容均与切片无关;这是第二个问题吗?
也许你遇到这个问题:
>> help(sh.row_slice)
有关模块xlrd.sheet中方法row_slice的帮助:
row_slice(self,rowx,start_colx = 0,end_colx =无)方法
xlrd.sheet。工作表实例
##
#返回给定行中Cell对象的切片。
>>>
sh.row_slice(rowx,lo,hi)给出与sh.row(rowx)相同的结果[lo:hi]
- 提供它是因为后者从长行中获得一个
的小切片效率很低。
如果你在使用切片的一般概念时遇到了麻烦,或许你可能想尝试Python教程。否则,请尝试更好地了解混乱是什么。
HTH,如果您愿意,请发电子邮件给我......
干杯,
John
John Machin写道:
kath写道:
如何在Python中读取Excel文件?
我找到了一个包来读取excel文件,可以在任何
平台上使用。
嗨Sudhir,
到目前为止,非常好:-)
http://www.lexicon.net/sjmachin/xlrd.htm
我安装并处理了这些示例,我发现它以不同的方式打印了单元格'
的内容。
>> import xlrd
>> book = xlrd.open_workbook(" Calculation_file.xls")
>> book = xlrd.open_workbook(" testbook.xls")
>> sh = book.sheet_by_index(0)
>> for range in range( sh.nrows):
print sh.row(rx)
[text:u''name'',text:u' '地址'',文字:u''ph'']
[text:u''sudhir'',text:u''bangalore'',number:1234.0]
[text:u''vinay'',text:u''bangalore'',number:3264.0]
如果您在屏幕上复制/粘贴
时会有什么问题呢?
在这种情况下
print sh.row(rx)
会出错;你必须输入
来表示范围内的rx .....
一行返回一系列Cell对象。你所看到的是
Python自动在行中的每个单元格上执行repr(单元格)。
Cell .__ repr__方法将其格式化为调试方式。以下是我的一个小测试文件中的一些
示例:
> import xlrd
bk = xlrd.open_workbook('sjm1.xls'')
sh = bk.sheet_by_index(0)
row0 = sh.row(0)
row0
[text:u''fubar'',number:1.0,number:2.0]
> firstcell = row0 [0]
type(firstcell)
< class''xlrd。 sheet.Cell '' >
> firstcell.ctype
1
> #cell type 1 is text
firstcell.value
u''fubar''
> repr(firstcell)
" text:u''fubar''"
我有点与切片混淆。帮助我....
以上所有与切片无关;这是第二个问题吗?
也许你遇到这个问题:
> help(sh.row_slice)
模块xlrd.sheet中方法row_slice的帮助:
row_slice(self,rowx,start_colx = 0,end_colx = None)方法
xlrd.sheet.Sheet实例
##
#返回给定行中Cell对象的切片。
>>
sh.row_slice(rowx,lo,hi)给出与sh.row(rowx)相同的结果[lo:hi]
- 提供它是因为后者从长行中获得
小片效率低。
如果遇到麻烦使用切片的一般概念,或许你可能想尝试Python教程。否则,请尝试更好地了解混乱是什么。
HTH,如果您愿意,请发电子邮件给我......
干杯,
John
您好,感谢您的回复。我只是花了一些时间阅读帮助文件和
来了解切片没什么用。但我确实在excel文件中有一个带有日期字段的
问题。
Excel文件中的日期(8/9/2006),我得到了当我在Excel中将日期值转换为通用格式时,我得到的值为38938.0,
。我想要实际的日期值
。怎么办?
谢谢。
问候,
Sudhir。
日期(8/9/2006)在Excel文件中,我得到的值为38938.0,
我在转换日期值时获得到Excel中的一般格式。我想要实际的日期值
。怎么做到的?
38938似乎是自19/1年1月1日以来的日期。我相信有人
可以帮助你弄清楚如何将其转换为更有用的价值。
-Matt
>
How do I read an Excel file in Python?
I have found a package to read excel file, which can be used on any
platform.
http://www.lexicon.net/sjmachin/xlrd.htm
I installed and working on the examples, I found its printing of cell''s
contents in a different manner.
>>import xlrd
book=xlrd.open_workbook("Calculation_file.xls" )
book=xlrd.open_workbook("testbook.xls")
sh=book.sheet_by_index(0)
for row in range(sh.nrows):
print sh.row(rx)
[text:u''name'', text:u''address'', text:u''ph'']
[text:u''sudhir'', text:u''bangalore'', number:1234.0]
[text:u''vinay'', text:u''bangalore'', number:3264.0]
I am bit confused with slicing. help me....
Thank you,
Regards,
Sudhir.
kath wrote:How do I read an Excel file in Python?
I have found a package to read excel file, which can be used on any
platform.Hi Sudhir,
So far, so good :-)
>
http://www.lexicon.net/sjmachin/xlrd.htm
I installed and working on the examples, I found its printing of cell''s
contents in a different manner.
>import xlrd
book=xlrd.open_workbook("Calculation_file.xls")
book=xlrd.open_workbook("testbook.xls")
sh=book.sheet_by_index(0)
for row in range(sh.nrows):
print sh.row(rx)
[text:u''name'', text:u''address'', text:u''ph'']
[text:u''sudhir'', text:u''bangalore'', number:1234.0]
[text:u''vinay'', text:u''bangalore'', number:3264.0]It helps when asking questions if you copy/paste exactly what is on
your screen;
in this case
print sh.row(rx)
would have given an error; you must have typed
for rx in range.....
A row is returned as a sequence of Cell objects. What you are seeing is
Python automatically doing repr(cell) on each cell in the row. The
Cell.__repr__ method formats it that way for debugging. Here are some
examples from a little test file of mine:
>>import xlrd
bk = xlrd.open_workbook(''sjm1.xls'')
sh = bk.sheet_by_index(0)
row0 = sh.row(0)
row0
[text:u''fubar'', number:1.0, number:2.0]
>>firstcell = row0[0]
type(firstcell)
<class ''xlrd.sheet.Cell''>
>>firstcell.ctype
1
>># cell type 1 is text
firstcell.value
u''fubar''
>>repr(firstcell)
"text:u''fubar''"
>
I am bit confused with slicing. help me....
None of the above is anything to do with slicing; is this a 2nd
problem?
Perhaps you are having trouble with this:>>help(sh.row_slice)
Help on method row_slice in module xlrd.sheet:
row_slice(self, rowx, start_colx=0, end_colx=None) method of
xlrd.sheet.Sheet instance
##
# Returns a slice of the Cell objects in the given row.>>>
sh.row_slice(rowx, lo, hi) gives the same result as sh.row(rowx)[lo:hi]
-- it is provided because the latter would be inefficient for getting a
small slice from a long row.
If you are having trouble with the general concept of slicing, perhaps
you might like to try the Python tutorial. Otherwise, please try to be
a bit more specific about what the confusion is.
HTH, and e-mail me if you prefer ...
Cheers,
John
John Machin wrote:kath wrote:How do I read an Excel file in Python?
I have found a package to read excel file, which can be used on any
platform.
Hi Sudhir,
So far, so good :-)
http://www.lexicon.net/sjmachin/xlrd.htm
I installed and working on the examples, I found its printing of cell''s
contents in a different manner.
>>import xlrd
>>book=xlrd.open_workbook("Calculation_file.xls" )
>>book=xlrd.open_workbook("testbook.xls")
>>sh=book.sheet_by_index(0)
>>for row in range(sh.nrows):print sh.row(rx)
[text:u''name'', text:u''address'', text:u''ph'']
[text:u''sudhir'', text:u''bangalore'', number:1234.0]
[text:u''vinay'', text:u''bangalore'', number:3264.0]
It helps when asking questions if you copy/paste exactly what is on
your screen;
in this case
print sh.row(rx)
would have given an error; you must have typed
for rx in range.....
A row is returned as a sequence of Cell objects. What you are seeing is
Python automatically doing repr(cell) on each cell in the row. The
Cell.__repr__ method formats it that way for debugging. Here are some
examples from a little test file of mine:
>import xlrd
bk = xlrd.open_workbook(''sjm1.xls'')
sh = bk.sheet_by_index(0)
row0 = sh.row(0)
row0
[text:u''fubar'', number:1.0, number:2.0]
>firstcell = row0[0]
type(firstcell)
<class ''xlrd.sheet.Cell''>
>firstcell.ctype
1
># cell type 1 is text
firstcell.value
u''fubar''
>repr(firstcell)
"text:u''fubar''"
I am bit confused with slicing. help me....None of the above is anything to do with slicing; is this a 2nd
problem?
Perhaps you are having trouble with this:>help(sh.row_slice)
Help on method row_slice in module xlrd.sheet:
row_slice(self, rowx, start_colx=0, end_colx=None) method of
xlrd.sheet.Sheet instance
##
# Returns a slice of the Cell objects in the given row.>>
sh.row_slice(rowx, lo, hi) gives the same result as sh.row(rowx)[lo:hi]
-- it is provided because the latter would be inefficient for getting a
small slice from a long row.
If you are having trouble with the general concept of slicing, perhaps
you might like to try the Python tutorial. Otherwise, please try to be
a bit more specific about what the confusion is.
HTH, and e-mail me if you prefer ...
Cheers,
John
Hi, thanks for the reply. I just took some time reading help file and
came to know to there is nothing do with slicing. But I do have a
problem with date field in the excel file.
the date( 8/9/2006 ) in Excel file, i am getting the value as 38938.0,
which I get when I convert date values to general format in Excel. I
want the actual date value. How do get that?
Thank you.
regards,
Sudhir.
the date( 8/9/2006 ) in Excel file, i am getting the value as 38938.0,which I get when I convert date values to general format in Excel. I
want the actual date value. How do get that?38938 appears to be the date in days since 1/1/1900. I''m sure someone
can help you figure out how to convert that to a more useful value.
-Matt
这篇关于如何在Python中读取Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!