xlsxwriter和Pycharm结果之间的“ for循环”有什么区别?

时间:2019-12-13 12:07:37

标签: python-3.x pycharm xlsxwriter

代码A:(PyCharm)

from functools import partial

from colorama import Fore,Style,init
init()


def x_in_y(word, inner):
    return inner in word




words = [
    ('pasport','passport'),
    ('limmit','limit'),
    ('putt','put')
]

sentence =['putt dweqrerwr','dfsdf putt','limmit','pasport']


for wrong,correct in words:
    filtered_names = filter(partial(x_in_y, inner=wrong), sentence)
    next_elem = next(filtered_names, None)
    if next_elem:
        print(f"Typo: {wrong} 'should be {correct}'")
        print(next_elem.replace(wrong, f"{Fore.RED}{wrong}{Style.RESET_ALL}"))
    for name in filtered_names:
        print(name.replace(wrong, f"{Fore.RED}{wrong}{Style.RESET_ALL}"))

代码B:(xlsxwriter)

import xlsxwriter

from functools import partial

from colorama import Fore,Style,init
init()


def x_in_y(word, inner):
    return inner in word

workbook = xlsxwriter.Workbook('C:\\Users\\Claude\\Desktop\\hello.xlsx')
worksheet = workbook.add_worksheet()
words = [
    ('pasport','passport'),
    ('limmit','limit'),
    ('putt','put')
]

sentence =['putt dweqrerwr','dfsdf putt','limmit','pasport']

row = 0


for wrong,correct in words:
    filtered_names = filter(partial(x_in_y, inner=wrong), sentence)
    next_elem = next(filtered_names, None)



    if next_elem:
        worksheet.write(row,0, f"Typo: {wrong} 'should be {correct}'")
        worksheet.write(row+1,0,next_elem.replace(wrong, f"{wrong}"))

    for name in filtered_names:
        worksheet.write(row+2,0,name)
        row += 1
workbook.close()

代码A的输出:

Typo: pasport 'should be passport'
pasport
Typo: limmit 'should be limit'
limmit
Typo: putt 'should be put'
putt dweqrerwr
dfsdf putt

代码B的输出: Output Code B

这两个代码对我来说看起来是一样的。

我不知道为什么“ for循环”不能在第二个代码中继续工作。 (xlsxwriter)

代码B的结果应为:(与代码A的结果完全一样)

Typo: pasport 'should be passport'
pasport
Typo: limmit 'should be limit'
limmit
Typo: putt 'should be put'
putt dweqrerwr
dfsdf putt

那是因为我将“ row = 0”放在错误的位置吗?

我已经尝试了多种更改代码顺序的方法,但是结果仍然与第一个代码不同。

有人可以给我一个提示或建议吗?

0 个答案:

没有答案