简体   繁体   English

如何从文件中拆分单词列表

[英]How can I split wordlist from a file

Hello guys I want to split words from a selected file but It returns ['0']大家好,我想从选定的文件中拆分单词,但它返回 ['0']

in my text file I have email:password like this email@gmail.com:password在我的文本文件中,我有 email:password 这样的 email@gmail.com:password

I want to split them into a list我想把它们分成一个列表

emails = ['email@gmail.com'] passwords = ['password]电子邮件 = ['email@gmail.com'] 密码 = ['密码]

path = easygui.fileopenbox()

print(path)
username = []
passwords = []

with open(path, 'r') as f:
    lineCount = len(f.readlines())
    lines = f.readlines()
    for line in str(lineCount):
        username = re.split(':',line)
        password = re.split(':',line)

Try doing it this way:尝试这样做:

import re
usernames = []
passwords = []

with open(path, 'r') as f:
    for line in f:
       line = line.strip()
       match = re.match(r'^.*?@\w+?\.\w+?:', line)
       if match:
           username = line[:match.end() - 1]
           password = line[match.end():]
           usernames.append(username)
           passwords.append(password)

Open the file and read one line at a time.打开文件并一次读取一行。 Split the line on colon which should give two tokens (assuming the file format is as stated in the question).在冒号上拆分应该给出两个标记的行(假设文件格式如问题中所述)。 Append each token to the appropriate list. Append 每个令牌到相应的列表。

usernames = []
passwords = []

with open(path) as data:
    for line in map(str.strip, data):
        try:
            pos = line.index(':')
            usernames.append(line[:pos])
            passwords.append(line[pos+1:])
        except ValueError:
            pass # ignore this line as no colon found
with open("test") as file:
    content=file.read()
    email,password=content.split(":")
print(email,"=======",password)  

代码

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

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