python基础——task9

练习题:

1、打开中文字符的文档时,会出现乱码,Python自带的打开文件是否可以指定文字编码?还是只能用相关函数?
  • linux使用’utf-8’编码方式,window使用’GBK’编码方式。
  • linux平台编码(UTF-8)与window平台(GBK)不一样。
  • 可以使用open(encoding=xx)进行转码
2、编写程序查找最长的单词

输入文档: res/test.txt

题目说明:

"""
Input file
   test.txt
   
Output file
   ['general-purpose,', 'object-oriented,']
   
"""
def longest_word(filename):
    # your code here
        pass
import os
def longest_word(filename):
    file = open(filename)
    words = file.readlines()
    file.close()
    for i in range(len(words)-1):
        words[i] =  words[i][:-2]
        
    max_str = []
    for i in range(len(words)):
        if [len == max(len_list) for len in len_list][i]:
            max_str.append(words[i])
    return max_str
longest_word('test.txt')
# ['general-purpose', 'object-oriented']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值