Reputation: 45
I'm trying to write a code that iterates through a txt file and only gets the lines I want to print them out.
the text file should look like so:
mimi
passwordmimi
mimi johnson
somejob
joji
passwordjoji
jojo
somejob
john
passwordjohn
jonathan
somejob
....
and so on. this text file contains basically a user information (for a log in). I need to make everyone's username print out and their real name (ex: mimi and mimi johnson.) and only those. I don't want the current user's info to print out (in this ex: joji)
here is my code:
username="joji"
file=open("username.txt","r")
x=file.readlines()
x=[item.rstrip('\n') for item in x]
x=iter(x)
for line in x:
if line==username:
next(x,None)
next(x,None)
next(x,None)
else:
print line + " username" ****username should print out. ex:mimi or john
next(x,None)
print line +" real name ****real name should print out. ex: mimi johnson or jonathan
for whatever reason when I run this program and i print out the second **** i put, it prints out the username's twice. (so ex:
mimi username
mimi real name
mimi johnson username
mimi johnson real name
john username
john real name
jonathan username
jonathan real name
....
why is that? it should print out
mimi username
mimi johnson real name
john username
jonathan realname
...
if someone could help me out i'd be really grateful i dont get python. Im also open to any other suggestions to do this.
EDIT::: i tried making a change with a suggestion this is the outcome:
new block of code:
else:
print line + "username"
line =next(x,None)
print line
this is the new outcome:
mimi username
passmimi real name
mimi johnson username
somejob real name
john username
passjohn real name
jonathan username
somejob real name(***im assuming this one is from john's job)
:/ its not doing what its supposed to
Upvotes: 0
Views: 1036
Reputation: 134
I would recommend using regex to parse this file:
import re
# regex expression to parse the file as you provided it
# you could access the parseddata as a dict using the
# keys "username", "password", "real_name" and "job"
ex = "\n*(?P<username>.+)\n(?P<password>.+)\n(?P<real_name>.+)\n(?P<job>.+)[\n\$]"
with open("usernames.txt", 'r') as users:
matches = re.finditer(ex, users.read())
for match in matches:
user = match.groupdict() # user is a dict
# print username and real name
print(user['username'], "username", user['real_name'], "real name")
Edit: I figured that regex was not really needed here as the format of this file is quite simple. So here is the same thing without using regex.
def parse(usersfile):
# strip line break characters
lines = (line.rstrip('\n') for line in usersfile)
# keys to be used in the dictionnary
keys = ('username', 'password', 'real_name', 'job')
while True:
# build the user dictionnary with the keys above
user = {key: line for key, line in zip(keys, lines)}
# yield user if all the keys are in the dict
if len(user) == len(keys):
yield user
else: # stop the loop
break
with open("usernames.txt", 'r') as usersfile:
for user in parse(usersfile):
# print username and real name
print(user['username'], "username", user['real_name'], "real name")
Upvotes: 1