How can I split wordlist from a file

Viewed 40

Hello guys I want to split words from a selected file but It returns ['0']

in my text file I have email:password like this email@gmail.com:password

I want to split them into a list

emails = ['email@gmail.com'] passwords = ['password]

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)
3 Answers

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.

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)  

code

Related