TypeError:print\u lol()得到意外的关键字参数“fh”

2024-05-19 18:19:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个nester.py文件:

"""This is the "nester.py" module, and it provides one function called
 print_lol which print lists that may or maynot include nested lists"""
import sys 
def print_lol(the_list, indent=False, level=0, fh=sys.stdout): 
    """This function takes a positional argument called "the_list" which is
    any python list (of , possibly, nested lists). each data item in the provided
    list is (recursively) printed to the screen on its own line.
    A second argument called level is used to insert tab-stops when a nested
    list is encountered"""

    for each_item in the_list: 
        if isinstance(each_item, list):
            print_lol(each_item, indent, level+1, fh)

        else:
            if indent:
                for tab_stop in range(level):
                    print("\t", end = "", file=fh)


        print(each_item, file=fh

以及一些实际使用它的代码:

*************************************************************
# This is my actual code

import nester 

man = []
other = []

try:
    data = open("sketch.txt")
    for each_line in data:
        try:
            (role, line_spoken) = each_line.split(":", 1)
            line_spoken = line_spoken.strip()
            if role == "Man":
                man.append(line_spoken)
            elif role == "Other Man":
                other.append(line_spoken)

        except ValueError:
            pass
    data.close()
except IOError:
    print("The data file is missing!")

try:
    with open("man_data.txt", "w") as man_file:
        nester.print_lol(man, fh=man_file)


    with open("other_data.txt", "w") as other_file:
        nester.print_lol(other, fh=other_file)


except IOError as err:
    print("File error: " + str(err))

我在这个社区看到过一些帖子,但是没有一个能帮我解决这个问题。我是Python的新手,在过去的四天里我一直停留在这个阶段。我不知道如何使我的功能发挥作用

我不知道我是否需要安装其他软件包。如果不使用nester,程序即使不整洁也可以正常工作


Tags: thedataislineitemlistnesterfile