Python iterates row data into another file - python-3.3

a python beginner here.
I come up with a small task so I could continue to learn and improve my python. I have an example file1:
Name: Group: UID:
user1 group1 12
user2 group2 23
user3 group3 34
user9 group9 99
I am trying to achieve the result in file2 column:
User_Name: User_ID:
user1 12
user9 99
I have gotten this far, and near the end is where I am stuck:
#!/usr/bin/python3.3
with open("file1") as rfile:
with open("file2", "w") as wfile:
for line in rfile:
l=line.strip()
if l:
x=l.split()
#From here, I am not sure what to do next.
if "user1" in x:
do something...
if "user9" in x:
do something..
Thanks for the help.

You can do
wfile.write( x[0] + "\t" + x[2] + "\n")

Related

How to use csv as instance attritubes in python?

How do I import csv file and use it as instance attributes for my class?
Here is the code ive written:
import random
import csv
class Cars:
def __init__(self, driver, team):
self.driver = driver
self.team = team
class Race:
def __init__(self, lap = 55):
self.lap = lap
self._finished = []
def race(self, list_of_cars):
for c in list_of_cars:
c.distance = 0
while list_of_cars:
for c in list_of_cars:
c.distance += random.randint(100,300)
if c.distance >= self.lap:
self._finished.append(c)
list_of_cars.remove(c)
def print_results(self):
print("Tournament Result\n" + "_" * 18)
for i, c in enumerate (self._finished):
print (i+1, c.driver, c.team)
cars_list = []
with open("driverandteam.csv",'r') as file:
csv_reader = csv.reader(file)
for line in csv_reader:
cars_list.append(Cars(line[0],line[1]))
r = Race(65)
cars = cars_list
r.race(cars)
r.print.results()
driverandteam.csv looks like this
Verstappen, Red Bull
Perez, Red Bull
Hamilton, Mercedes
Bottas, Mercedes
Leclerc, Ferrari
Sainz, Ferrari
Ricciardo, McLaren
Norris, McLaren
Ocon, Alphine
Alonso, Alphine
Tsunoda, AlphaTauri
Gasly, AlphaTauri
Vettel, Aston Martin
Stroll, Aston Martin
Latifi, Williams
Russell, Williams
Raikkonen, Alfa Romeo
Giovinazzi, Alfa Romeo
Mazepin, Haas
Schumacher, Haas
I keep getting "list of out range" error in line 36 of my code but im not understanding why. How can I fix my code so it works.
I ran your code locally and with the provided csv (thanks for providing all the needed info!), I had to make one small code change at the end:
r.print_results()
After that it all ran as expected and I got the following output:
Tournament Result
__________________
1 Verstappen Red Bull
2 Hamilton Mercedes
3 Leclerc Ferrari
4 Ricciardo McLaren
5 Ocon Alphine
6 Tsunoda AlphaTauri
7 Vettel Aston Martin
8 Latifi Williams
9 Raikkonen Alfa Romeo
10 Mazepin Haas
11 Perez Red Bull
12 Sainz Ferrari
13 Alonso Alphine
14 Stroll Aston Martin
15 Giovinazzi Alfa Romeo
16 Bottas Mercedes
17 Gasly AlphaTauri
18 Schumacher Haas
19 Norris McLaren
20 Russell Williams
So I'm unable to reproduce the error you're receiving, let us know if you're still encountering this error and can update any code/data to help reproduce.

Create multiple possible email addresses based on names in Python

Given a dataframe as follows:
firstname lastname email_address \
0 Doug Watson douglas.watson#dignityhealth.org
1 Nick Holekamp nick.holekamp#rankenjordan.org
2 Rob Schreiner rob.schriener#wellstar.org
3 Austin Phillips austin.phillips#precmed.com
4 Elise Geiger egeiger#puracap.com
5 Paul Urick purick#diplomatpharmacy.com
6 Michael Obringer michael.obringer#lashgroup.com
7 Craig Heneghan cheneghan#west-ward.com
8 Kathy Hirst kathleen.hirst#sunovion.com
9 Stefan Bluemmers stefan.bluemmers#grunenthal.com
companyname
0 Dignity Health
1 Ranken Jordan Pediatric Bridge Hospital
2 WellStar Health System
3 Precision Medical Products, Inc.
4 puracap.com
5 Diplomat Specialty Pharmacy
6 Lash Group
7 West-Ward Pharmaceuticals
8 Sunovion Pharmaceuticals
9 GrĂ¼nenthal Group
How could I create possible email addresses using common email patterns as such: firstlast#example.com, first.last#example.com, f.last#example.com, lastF#example.com, first_last#example.com, firstL#example.com, etc.
df['email1'] = df.firstname.str.lower() + '.' + df.lastname.str.lower() + '#' + df.companyname.str.replace('\s+', '').str.lower() + '.com'
print(df['email1'])
Out:
0 doug.watson#dignityhealth.com
1 nick.holekamp#rankenjordanpediatricbridgehospi... --->problematic
2 rob.schreiner#wellstarhealthsystem.com
3 austin.phillips#precisionmedicalproducts,inc..com --->problematic
4 elise.geiger#puracap.com.com --->problematic
...
9995 terry.hanley#kempersportsmanagement.com
9996 christine.marks#geocomp.com
9997 darryl.rickner#doe.com
9998 lalit.sharma#lovelylifestyle.com
9999 parul.dutt#infibeam.com
Some of them seems quite problematic, anyone could help to solve this issue? Thanks a lot.
EDITED:
print(df) after applying #Sajith Herath's solution:
Out:
firstname lastname companyname \
0 Nick Holekamp Ranken ...
email
0 nick. ...
You can use a method to create permutations of username with different separators and define a max length that simplify the domain using company name as follows
import pandas as pd
import random
data = {"firstname":["Nick"],"lastname":["Holekamp"],"companyname":["Ranken \
Jordan Pediatric Bridge Hospital"]}
df = pd.DataFrame(data=data)
max_char = 5
emails = []
def simplify_domain(text):
if len(text)>max_char:
text = ''.join([c for c in text if c.isupper()])
return text.lower()
return text.replace("\s+","").lower()
def username_permutations(first_name,last_name):
# define separators
separators = [".", "_", "-"]
#lower case
combinations = list(map(lambda x:f"{first_name.lower()}{x} \
{last_name.lower()}",separators))
#append a random number to tail
n = random.randint(1, 100)
combinations.extend(list(map(lambda x:f"{x}{n}",combinations)))
return combinations
for index,row in df.iterrows():
usernames = username_permutations(row["firstname"],row["lastname"])
email_permutations = list(map(lambda x: f" \
{x}#{simplify_domain(row['companyname'])}.com",usernames))
emails.append(','.join(email_permutations))
df["email"] = emails
Final result will be nick.holekamp#rjpbh.com,nick_holekamp#rjpbh.com,nick-holekamp#rjpbh.com,nick.holekamp66#rjpbh.com,nick_holekamp66#rjpbh.com,nick-holekamp66#rjpbh.com
you can modify simplify_domain method to validate given string such as removing inc or .com values

Printing list in different columns

I am quite new to Python and I am now struggling with printing my list in columns. It prints my lists in one columns only but I want it printed under 4 different titles. I know am missing something but can't seem to figure it out. Any advice would be really appreciated!
def createMyList():
myAgegroup = ['20 - 39','40 - 59','60 - 79']
mygroupTitle = ['Age','Underweight','Healthy','Overweight',]
myStatistics = [['Less than 21%','21 - 33','Greater than 33%',],['Less than 23%','23 - 35','Greater than 35%',],['Less than 25%','25 - 38','Greater than 38%',]]
printmyLists(myAgegroup,mygroupTitle,myStatistics)
return
def printmyLists(myAgegroup,mygroupTitle,myStatistics):
print(': Age : Underweight : Healthy : Overweight :')
for count in range(0, len(myAgegroup)):
print(myAgegroup[count])
for count in range(0, len(mygroupTitle)):
print(mygroupTitle[count])
for count in range(0, len(myStatistics)):
print(myStatistics[0][count])
return
createMyList()
To print data in nice columns is nice to know Format Specification Mini-Languag (doc). Also, to group data together, look at zip() builtin function (doc).
Example:
def createMyList():
myAgegroup = ['20 - 39','40 - 59','60 - 79']
mygroupTitle = ['Age', 'Underweight','Healthy','Overweight',]
myStatistics = [['Less than 21%','21 - 33','Greater than 33%',],['Less than 23%','23 - 35','Greater than 35%',],['Less than 25%','25 - 38','Greater than 38%',]]
printmyLists(myAgegroup,mygroupTitle,myStatistics)
def printmyLists(myAgegroup,mygroupTitle,myStatistics):
# print the header:
for title in mygroupTitle:
print('{:^20}'.format(title), end='')
print()
# print the columns:
for age, stats in zip(myAgegroup, myStatistics):
print('{:^20}'.format(age), end='')
for stat in stats:
print('{:^20}'.format(stat), end='')
print()
createMyList()
Prints:
Age Underweight Healthy Overweight
20 - 39 Less than 21% 21 - 33 Greater than 33%
40 - 59 Less than 23% 23 - 35 Greater than 35%
60 - 79 Less than 25% 25 - 38 Greater than 38%

After a seperator there is a key in loop. How to keep it?

---------------------------
CompanyID: 000000000000
Pizza: 2 3.15 6.30
spaghetti: 1 7 7
ribye: 2 40 80
---------------------------
CompanyID: 000000000001
burger: 1 3.15 6.30
spaghetti: 1 7 7
ribye: 2 40 80
--------------------------
I'm doing a for loop over a list of lines. Every line is an item of a list. I need to keep the companyID while looking for a user input.
While this is printing the variable x=True. I cant take company ID to print it.
a='-'
for line in lines:
if a in line:
companyID= next(line)
if product in line:
x=True
TypeError: 'str' object is not an iterator
You can use your line seperator to identify when new data starts. Once you see the line with "----" then you can start collecing info in a new dictionary. for each line take its key and value by splitting on ":" and create the entry in the dictionary.
When you see the next "----" line you know thats the end of the data for this company so then do your check to see if they have the product and if so print the company id from the dictionary.
line_seperator_char = '-'
company_data = {}
product = 'burger'
with open('data.dat') as lines:
for line in lines:
line = line.rstrip()
if line.startswith(line_seperator_char):
if product in company_data:
print(f'{company_data["CompanyID"]} contains the product {product}')
company_data = {}
else:
key, value = line.split(':')
company_data[key] = value
OUTPUT
000000000001 contains the product burger
No it doesnt run. Could you explain what does "[1] means near split()[1]?
Another try that doesnt run is
y=[]
y=lines[1].split(' ')
for line in lines:
y=line.split(' ')
if len(y[1])==10:
companyID=y[1]
if product in line:
x=True
Thanks for the answers.Something that finally worked in my case was that:
y=[]
y=line[1].split(' ')
a='-'
for line in lines:
if line.startswith("CompanyID:"):
y=line.split(' ')
companyID=y[1]
if product in line:
x=True

edit two parts of text document python

Similar to my recent question asked:
I have a text file contain some data using this piece of code
def Add_score():
with open("users.txt") as myFile:
for num, line in enumerate(myFile, 1):
if name in line:
line_found = num
break
It finds the line that has a specific name. The line would look like this.
Name: whatever Username: whatever password: whatever score: 25 goes: 3
I need to be able to add number to score as well as goes
Change 3 to 4 and change 25 to 26
Here you are:
line = 'Name: Username: password: whatever score: 25 goes: 3'
print(line)
lineSplitted = line.split()
print(lineSplitted)
updatedLine = " ".join(lineSplitted[0:5] + [str(int(lineSplitted[5])+1)] + [lineSplitted[6]] + [str(int(lineSplitted[7])+1)])
print(updatedLine)
prints:
Name: Username: password: whatever score: 25 goes: 3
['Name:', 'Username:', 'password:', 'whatever', 'score:', '25', 'goes:', '3']
Name: Username: password: whatever score: 26 goes: 4

Resources