I have a script for creating accounts that outputs the following:
creating user in XYZ: username: testing firstName: Bob lastName:Test email:auto999#nowhere.com password:gWY6*Pja&4
So, I need to create a python script that will store the username and password in a csv file.
I tried splitting this string by spaces and colons then indexing it, but this isn't working quite properly and could fail if the message is different. Does anyone have any idea how to do this?
Regex is almost always the answer to this type of issue:
import re
text = 'creating user in XYZ: username: testing firstName: Bob lastName:Test email:auto999#nowhere.com password:gWY6*Pja&4'
pattern = '.*username:\s*(\S+)\s*firstName:\s*(\S+)\s*lastName:\s*(\S+)\s*email:\s*(\S+)\s*password:\s*(\S+)'
values = re.findall(pattern, text)
print(values)
Output:
[('testing', 'Bob', 'Test', 'auto999#nowhere.com', 'gWY6*Pja&4')]
Regexr Pattern Explanation
I don't see the need for Regex here, a simple but robust parsing is enough:
def get_data(account: str, attribute: str) -> str:
data = ' '.join(account.split()).strip()
for k, v in {' :': ':', ' : ': ':', ': ': ':'}.items():
data = data.replace(k, v)
index1 = data.find(attribute)
index2 = data.find(' ', index1)
return data[index1 + len(attribute + ':'): len(account) if index2 == -1 else index2]
example of use:
acc = "username: testing firstName: Bob lastName:Test email:auto999#nowhere.com password:gWY6*Pja&4"
print(get_data(acc, 'username'))
print(get_data(acc, 'password'))
output:
testing
gWY6*Pja&4
As the generator is yours, you can control how the accounts are created and I personally think that Regex is not easy to maintain.
This approach works even adding extra spaces or changing the order of the attributes, e.g.:
acc = " username: testing firstName: Bob lastName :Test email:auto999#nowhere.com password : gWY6*Pja&4 "
acc = "firstName: Bob username: testing email:auto999#nowhere.com password:gWY6*Pja&4 lastName:Test "
Related
The issue I have is that I'm not able to get the correct part of my txt string. The txt string also has to be in the current order, I can't switch role and name.
txt = \
'''
Company name
leader: Claire
cashier: Ole
'''
def role(name):
start= txt.find(name)-len(name)
end= txt.find('\n', start)
return txt[start:end]
If I type role("Ole") I expect the output to be 'cashier'.
The output I am getting though is 'r: Ole'.
You can create a dictionary that associates the names to the correspondent roles, so that it becomes very easy to retrieve them even in a much longer list:
txt = \
'''
Company name
leader: Claire
cashier: Ole
'''
t = txt.split()
roles = {t[i] : t[i-1].strip(':') for i in range(3, len(t), 2)}
def role(name):
return roles[name]
Once set up, it's pretty intuitive.
stringy = "cashier: Ole"
stringy.partition(':')[0]
you can use .partition and bring only the first element
this will work but if there is anything diff about your string it may break down.
stringy = """ Company name
leader: Claire
cashier: Ole"""
def role(name):
slice = stringy.partition(name)[0]
role_str = slice.split(' ')[-2]
return role_str
I'm trying to create a 'check' system for a password generator that will advsie whether or not three of the same types of character family are found in a row in a generated password, i.e
If the password is
y8kpBD8zcZLKRSh1j7vwCMDQ5orR8VEP
it will find 'ZLK' etc
I first thought lowercase_repeat = re.compile("[a-z]{3}") would for example find three lowercase repeats, but I can't seem to understand how this works exactly.
The password generator is below:
import random
import re
generator = random.SystemRandom()
password_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!##$%^&*()'
password = ''.join(generator.choice(password_characters) for _ in range(32))
print password
If you just want to check for specific character sets; e.g: all uppercase, all lowercase, digit and non-alnum - you can create a non-capturing group for each set. For example:
import re
pattern = '(?:[a-z]{3}|[A-Z]{3}|\d{3}|[\x20-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]{3})'
password = 'y8kpBD8zcZLKRSh1j7vwCMDQ5orR8VEP!'
matches = re.search(pattern, password)
The variable matches returns None if there are no matches, indicating the password passes.
The pattern [\x20-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E] is a (probably pretty gnarly) way to catch a set of all non-alnum ascii characters (hex codes). It represents the following set:
[space] ! " # $ % & ' ( ) * + , - . / : ; < = > ? # [ \ ] ^ _ ` { | } ~
I pulled it out of an old project, so YMMV. I'm sure there might be a more succinct way to express it - indeed, you might prefer to explicitly specify a set; e.g: [!?#] etc.
Quick sanity-check:
import re
def check_password(password):
pattern = '(?:[a-z]{3}|[A-Z]{3}|\d{3}|[\x20-\x2F\x3A-\x40\x5B-\x60\x7B-\x7E]{3})'
return re.search(pattern, password)
passwords = ['a', 'abc', 'ABC', 'aBc', '1bc', '123']
for password in passwords:
if check_password(password):
print 'password failed: ', password
else:
print 'password passed: ', password
Yields:
password passed: a
password failed: abc
password failed: ABC
password passed: aBc
password passed: 1bc
password failed: 123
Hope this helps :)
I would like to remove a comma from a string in Python. This is the code that I am using:
next_column = unicode(my_value)
next_column.replace(",", " ")
next_column.translate(dict([[ord(char), u''] for char in u',']))
next_column.translate(dict([[ord(char), None] for char in u',']))
if my_key == "practice_name":
nc = str(next_column)
nc.replace(",", " ")
nc.replace(',', " ")
pprint(nc)
The pprint shows:
'Phoenix Electronics, LLC'
The comma is still there.
"my_value" comes from a Postgres database, an old legacy app where the company failed to check the encoding of what was going into the database.
I don't know if this is a code issue or an encoding issue. Does anyone have any suggestions?
Try on the same line like,
>>>>import string
>>>>transtab = string.maketrans(",", " ")
>>>>unicodestring = r'Phoenix Electronics, LLC'
>>>>unicodestring.translate(transtab)
'Phoenix Electronics LLC'
I am trying to write a login routine for a python script. In doing so, I find the need to pattern match the credentials on a whole word basis. I have attempted to RegEx this, but it is failing for reasons that are unclear to me, but I hope are obvious to someone here. The code and output:
import re
authentry = "testusertestpass"
username = "testuser"
password = "testpass"
combo = "r\'\\b"+username + password + "\\b\'"
testcred = re.search(combo, authentry)
print combo
print authentry
print testcred
r'\btestusertestpass\b'
testusertestpass
None
So my regex test appears, at least to me, to be properly formatted, and should be a direct match against the test string, but is not. Any ideas? Thanks so much for any insight!
try this: it may works.
import re
authentry = "testusertestpass with another text"
username = "testuser"
password = "testpass"
combo = username + password + r'\b'
testcred = re.search(combo, authentry)
print combo
print authentry
print testcred
output:
testusertestpass\b
testusertestpass with another text
<_sre.SRE_Match object at 0x1b8a030>
Here's my code:
start_j = raw_input('Enter a name: ')
start_j = start.replace("A", "J")
start_j = start.replace("B", "J")
start_j = start.replace("C", "J")
print "Your name is " + start_j
Is there anyway to put all the alphabets in one list so that I wouldn't have to repeat the same process again and again until I reach letter "Z"
I tried using loops, but I still can't seem to get the right way to do it.
Here's a scenario:
The user will be prompted to input a name.
If the name contains a letter other than "J", it will be automatically replaced using the replace() function.
Hence it will print out the input starting with J
Here's an example:
site = raw_input('Enter your website: ')
site = site.replace("http://", "")
site = site.replace("https://", "")
site = site.replace("ftp://", "")
print "Your website is: " + site
An expected input would be http://www.google.com
So the expected out would become:
Enter your website: http://www.google.com
Your website is: www.google.com
I'm looking for a way to put "http://", "https://", "ftp://" all in one list so I wouldn't have to enter
site = site.replace("something", "something)
many times
You could use a regex to replace all of the letters at once:
>>> import re
>>> re.sub(r'[A-Z]', 'J', 'This Is A Test Name')
'Jhis Js J Jest Jame'
(After edit): You can use .startswith() and string slicing:
>>> name = 'A Name'
>>>
>>> if not name.startswith('J'):
... name = 'J' + name[1:]
...
>>> name
'J Name'
Although I'm not sure why you'd even need to check with .startswith(). Either way, the result will be the same.
You can use this:
remove_from_start = ["http://", "https://", "ftp://"]
for s in remove_from_start:
if site.startswith(s):
site = site[len(s):]
break
Or a regular expression based solution:
import re
regex = '^(https?|ftp)://'
site = re.sub(regex, '', site)
import re
site = raw_input('Enter your website: ')
# input http://www.google.com or https://www.google.com or ftp://www.google.com
site = re.sub('^(?:https?|ftp)://', '', site)
print "Your website is: " + site
use a dictionary:
In [100]: import string
In [101]: dic=dict.fromkeys(string.ascii_uppercase,"J")
In [104]: start_j = raw_input('Enter a name: ')
Enter a name: AaBbCc
In [105]: "".join(dic.get(x,x) for x in start_j)
Out[105]: 'JaJbJc'
Edit:
In [124]: dic={"https:":"","http:":"","ftp:":""}
In [125]: strs="http://www.google.com"
In [126]: "".join(dic.get(x,x) for x in strs.split("//"))
Out[126]: 'www.google.com'
use re, dict and lambda:
import re
replacte_to = {
"http://": "",
"https://": "",
"ftp://": "",
}
re.sub("^(ht|f)tps?://", lambda match: replacte_to[match.group(0)], YOUR_INPUT_STRING)