1、打开中文字符的文档时,会出现乱码,Python自带的打开文件是否可以指定文字编码?还是只能用相关函数?
open(file, mode=‘r’, buffering=None, encoding=None, errors=None, newline=None, closefd=True)
encoding :编码方式,默认为’gbk’,可将其改为’utf-8’.
即使用open(path, ‘r’, encoding = ‘utf-8’) 来打开中文字符文件。
2、编写程序查找最长的单词
输入文档: res/test.txt
题目说明:
“”"
Input file
test.txt
Output file
[‘general-purpose,’, ‘object-oriented,’]
“”"
def longest_word(filename):
with open(filename, 'r') as f:
data = f.read().split('\n')
words = ','.join(data)
words = list(words.split(','))
words = sorted(words, key = lambda x:len(x))
lonword = [i for i in words if len(i) == len(words[-1])]
return lonword
lonword=longest_word('test.txt')
print(lonword)