<jupyter_start><jupyter_text>学习 “yield”  结合xlwt写入xls文件<jupyter_code>from xlwt import Workbook, easyxf
from loguru import logger
import os

wb = Workbook(encoding='utf-8')
ws = wb.add_sheet('merged', cell_overwrite_ok=True)

style = easyxf('align: vertical center, horizontal center;')
row_cursor = 5

# 定义生成器函数
def write_row(max_rows):
    row = 0
    while row <= max_rows:
        # 在 行号 = row 写入一行内容
        ws.write_merge(row, row, 0, 11, '行: {}'.format(row + 1), style)
        # 行号 + 1
        row += 1
        # 如果 行号 匹配, 暂停并返回
        if row in [10, 20 ,30 ,40]:
            logger.debug('行号匹配 暂停: {}', row)
            _row = yield row
            logger.debug('Get generator send value: {}', _row)
            # 设定 恢复后的新 行号
            row = _row
        logger.debug('新行号: {}', row)<jupyter_output><empty_output><jupyter_text>迭代一个生成器的方法For example:```for x in write_gen```Note:- 第一次调用时，请使用`next()`语句或是`send(None)`- use `__next__()` or `next(your_generator_instance)` in python3<jupyter_code>write_gen = write_row(max_rows=11)

# init x, for specially handling in the first call generator
x = next(write_gen)
while True:
    try:
        x = write_gen.send(x + 1)
    except StopIteration:
        break
    else:
        logger.debug('Get yield value: {}', x)

# for row in write_gen:
#     logger.debug('Get yield value: {}', x)
#     write_gen.send(row + 1)

        
logger.debug('Loop ended.')
os.chdir('../MyNotebook')
wb.save('yield.xls')<jupyter_output>2021-04-26 11:56:21.122 | DEBUG    | __main__:write_row:26 - 新行号: 1
2021-04-26 11:56:21.123 | DEBUG    | __main__:write_row:26 - 新行号: 2
2021-04-26 11:56:21.124 | DEBUG    | __main__:write_row:26 - 新行号: 3
2021-04-26 11:56:21.127 | DEBUG    | __main__:write_row:26 - 新行号: 4
2021-04-26 11:56:21.128 | DEBUG    | __main__:write_row:26 - 新行号: 5
2021-04-26 11:56:21.130 | DEBUG    | __main__:write_row:26 - 新行号: 6
2021-04-26 11:56:21.131 | DEBUG    | __main__:write_row:26 - 新行号: 7
2021-04-26 11:56:21.131 | DEBUG    | __main__:write_row:26 - 新行号: 8
2021-04-26 11:56:21.133 | DEBUG    | __main__:write_row:26 - 新行号: 9
2021-04-26 11:56:21.133 | DEBUG    | __main__:write_row:21 - 行号匹配 暂停: 10
2021-04-26 11:56:21.135 | DEBUG    | __main__:write_row:23 - Get generator send value: 11
2021-04-26 11:56:21.136 | DEBUG    | __main__:write_row:26 - 新行号: 11
2021-04-26 11:56:21.137 | DEBUG    | __main__:write_row:26 - 新行号: 12
2021-04-26 11:56:21.138 | DEBUG    | __main__:<module>:18 - Loop ended.<jupyter_text>列表的笛卡尔积, 排列组合两个列表<jupyter_code>import itertools

def createDeck():
    I=[1,2,3,4,5,6,7,8,9,10]
    J=['spades','hearts','diamonds','clubs']
    return (itertools.product(I,J))

prod = createDeck()
for (a, b) in prod:`
    print(a, b)<jupyter_output>1 spades
1 hearts
1 diamonds
1 clubs
2 spades
2 hearts
2 diamonds
2 clubs
3 spades
3 hearts
3 diamonds
3 clubs
4 spades
4 hearts
4 diamonds
4 clubs
5 spades
5 hearts
5 diamonds
5 clubs
6 spades
6 hearts
6 diamonds
6 clubs
7 spades
7 hearts
7 diamonds
7 clubs
8 spades
8 hearts
8 diamonds
8 clubs
9 spades
9 hearts
9 diamonds
9 clubs
10 spades
10 hearts
10 diamonds
10 clubs<jupyter_text>生成列表[(61, 0, 0), (122, 0, 0)], 分页参数<jupyter_code>def page_breaks(row_count_per_page):
    n = 1
    yield n * row_count_per_page
    n += 1

row_count_per_page = 61
l = [(i * row_count_per_page, 0, 0) for i in range(1, 11)]
l<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13418625409104434, 'fraction_numerical': 0.11246652781910146, 'mean_word_length': 3.441215323645971, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '65781', 'license': 'Apache-2.0', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1463, 'n_words': 341, 'path': 'xlwt-expirement.ipynb', 'repo_name': 'madmadcat/MyNotebook', 'size': '3361'}