首页 > 解决方案 > 将带有值的文本文件加载到 Python 中的元组中

问题描述

我有file.txt这样的值的文本文件:

word1
word2
word3
word4
word5

我想要的是 Python 3 中这样的元组:

my_tuple = ('word1','word2','word3','word4','word5')

有什么建议吗?

标签: pythonpython-3.xfiletuplesfile-handling

解决方案


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')

推荐阅读