简体   繁体   English

如何在python中使用冒号分隔文本文件中的字符串?

[英]How would I separate strings in a text file with a colon in python?

How would I loop through a text file and store a value. 我如何循环文本文件并存储值。 If I take a line from a text file, hello@aol.com:Password1, How would I store email as hello@aol.com , and password as Password1 ? 如果我从文本文件中取一行,hello @ aol.com:Password1,我如何将电子邮件存储为hello@aol.com,将密码存储为Password1?

file = open("TEST.txt", "r")

for line in file:
    print(line)

This is a useful way to "open files": 这是“打开文件”的有用方法:

with open('TEST.txt', 'r') as f:
    f.readlines()

to split strings, you could use the split method: 要分割字符串,您可以使用split方法:

'hello@aol.com:Password1'.split(':')

(the split method takes "what to split by" as an optional parameter) (split方法将“拆分的内容”作为可选参数)

As Bruno commented, you should take a look at string methods 正如Bruno评论的那样,你应该看看字符串方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM