使用Openpyxl和python3.5,我尝试使用下标从excel工作表中获取第一行但我错了。
# after getting filename
# after loading worksheet
# to get the first row of the worksheet
first_row = worksheet.rows[0]
# I get
Traceback (most recent call last):
File "<pyshell#54>", line 1, in <module>
first_row = phc_th_sheet.rows[1]
TypeError: 'generator' object is not subscriptable
关于获得第一行,我也尝试过 first_row =工作表。(row = 1) #和 first_row = worksheet.rows [:1]
没有用。任何建议或openpyxl中没有的功能? 我已经访问了https://openpyxl.readthedocs.io/en/default/的文档,但我发现没有什么有用的索引和选择行
答案 0 :(得分:19)
我终于在文档中找到了答案:
global $post;
//for product cat archive page only
$term = get_queried_object();
$cutomPageImageOption = get_option('taxonomy_' . $term->term_id);
$cutomPageImage = $cutomPageImageOption['cat_head_link'];
if ($cutomPageImage > 1) { echo "Please add a category head image in the admin panel"; }
?>
<section id="page-header" class="page-header" style="background-image: url('<?php echo $cutomPageImage; ?>">
<div class="container">
<div class="row">
<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
<h1><?php woocommerce_page_title(); ?></h1>
<?php endif; ?>
</div>
</div>
</section>
这对我有用。
答案 1 :(得分:11)
错误TypeError: 'generator' object is not subscriptable
。意味着你试图通过索引来访问一个生成器,它没有生成器,因为它会在你迭代它时创建元素。
您可以轻松解决此问题,将其转换为列表以获取所需的元素:
first_row = list(worksheet.rows)[0]
或迭代思考行:
for row in worksheet.rows:
foo(row)
这是因为,即使两者都是可迭代的,列表和生成器的行为也可能完全不同,您可以在此处更好地解释:
https://wiki.python.org/moin/Generators
https://docs.python.org/3/library/stdtypes.html#iterator-types
https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range