Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我刚刚从文件'text.txt'中读取了第 10 行
>>>line=linecache.getline("text.txt",10) >>>line "['\\x02', '\\x03']\n"
在这种情况下,我想创建一个列表lst有两个变量 '\\x02' 和 '\\x03'
>>>lst ['\\x02','\\x03']
我必须迭代不同文本行的过程,这些文本行总是像行一样格式化,也有更多变量。
有什么建议么?
谢谢
这将采用具有任意数量元素的该格式的字符串并将其转换为列表。
line = "['\\x02', '\\x03']\n" line = line.strip()[1:-1] lst = [x.strip()[1:-1] for x in line.split(",")]