python读取文件内容

定义一个名为 get_words_from_file(filename)的函数,该函数将filename作为参数。该函数读取参数中指定的文件的内容,并返回单词列表。文件中的单词由空格分隔。文件内容:life is a long journey with problems solve lessons learn but most of all experiences to enjoy
测试: print(get_words_from_file("input2.txt"))
结果: ['the', 'heavy', 'rain', 'is', 'to', 'ease', 'tonight', 'however', 'further', 'showers', 'are', 'expected', 'tomorrow']
def get_words_from_file(filename):

你题目的解答代码如下:

def get_words_from_file(filename):
    with open(filename, 'r', encoding='utf-8') as fileObj:
        s = fileObj.read()
    return s.split()

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632