本文介绍了用python打开txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要打开一个txt文件.在txt文件中我有
i need to open a txt file .In txt file i have
Andrei:Popescu:Bucuresti
Maria:Popescu:Targu-Mures
....
如何将文本文件读入三个变量并为每一行做一些事情?对不起我的英语.
How do I read a text file into three variable and for each line do something ?Sorry for my english.
推荐答案
注意名称之间用冒号分隔(:
) 所以在中添加
将它们拆分并将它们存储在多个变量中::
split()
Notice that the names are separated by a colon(:
) so add :
in split()
to split them and store them in multiple variables:
with open("filename.txt") as f:
for line in f :
word1,word2,word3 = line.split(":")
print(word1)
print(word2)
print(word3)
这篇关于用python打开txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!