file.txt 价值观如下:
file.txt
word1 word2 word3 word4 word5
我想要的是Python 3中这样的元组:
my_tuple = ('word1','word2','word3','word4','word5')
您可以创建 list tuple
list
tuple
import os.path text_file = open("file.txt", encoding="utf8") my_list = [] for line in text_file: my_list.append(line) my_tuple = tuple(my_list) print(my_tuple) print(type(my_tuple))
with open('file.txt','r') as f: tup = tuple(f.read().split('\n')) tup ('word1', 'word2', 'word3', 'word4', 'word5')
with open('file.txt','r') as f: my_tuple=tuple(line.strip('\n') for line in f) print(my_tuple) # ('word1','word2','word3','word4','word5')