我无法打开将多个文本文件中的文本放入列表中。要引入的文本由程序显示后的参数表示,所以我需要使用sys.argv[1:]
但是我遇到了错误:
File "testa.py", line 19, in organize_data
with open(sys.argv[1:],'r') as f:
TypeError: invalid file: ['test.txt', 'test2.txt']
将sys.argv[1:]
更改为sys.argv[1]
只需一个文本文件即可正常运行。我不知道如何将2个或更多文本文件合并到一个列表中。
import sys
sys.argv
for argument in sys.argv:
print("argument = " , argument)
print ("sys.argv = ", sys.argv)
print("first list item = " , sys.argv[ 0 ])
print("second list item = " , sys.argv[ 1 ])
files_used = sys.argv[1:]
def organize_data():
word_list =[]
bad_chars = ''',./<>?;:'"[{]}\|!@#$%^&*()_+-='''
with open(files_used,'r') as f:
for line in f:
for word in line.split():
word = word.strip(bad_chars)
if word:
try:
_ = float(word)
except ValueError:
word_list.append(word.lower())
return print(word_list)
organize_data()