我是 Python 文件的新手,在删除输出控制台中的 '\n' 和单词None 时遇到了问题。这是我的代码:def function(inputFile, wordFile): input = open(inputFile, 'r') words = open(wordFile, 'r') wordList = [] for line in words: wordList.append(line.split(',')) print(wordList) words.close()##call functionresult = function("file1.txt","file2.txt")print(result)print()我的 file2.txt/wordFile/words 看起来像这样:你好世界123,456这是我得到的输出:['你好', '世界\n']['123', '456\n']没有任何我知道发生了很多事情,但是如何删除 '\n' 和None?
1 回答

Helenr
TA贡献1780条经验 获得超4个赞
要摆脱空白字符,您可以使用strip
:
wordList.append(line.strip().split(','))
此外,您的函数不不返回任何东西,所以result = function("file1.txt","file2.txt")
将分配什么来result
也叫None
Python编写的。要让它返回一些东西,请return
在函数的末尾使用:
return wordlist
也可以返回多个变量:
return var1, var2, ...
你可以通过
a, b, ... = function(..)
添加回答
举报
0/150
提交
取消