File Handling 2 1
File Handling 2 1
try:
my_file_handle=open("folder/test.txt")
except IOError:
print("File not found or path is incorrect")
finally:
print("exit")
Access Modes
read-only mode
write-only mode
append mode
both read and write mode
Reading from a text file
import pickle
pickle.dump() to write on a file
import pickle
L1=[1,2,3,4,5]
F=open(‘d:\\filelist.dat’,wb)
pickle.dump(L1,F)
print ( “list written on file”)
F.close()
pickle.load () to read from a file
import pickle
list1 = {'m':12,'n':23,'t':34,'s':45,'u':56}
f1= open("d:\\mfile2.txt", 'wb')
pickle.dump(list1,f1) #writing content on the file
print ( "written")
f1.close()
print ( '***Reading from a file***')
f2= open ("d:\\mfile2.txt",'rb')
m1=pickle.load(f2) // print (pickle.load(f2))
print (m1)
print ( '****************')
f2.close()
output
RESTART: C:\Users\admin\AppData\Local\Programs\Python\Python37-32\
readwritedicttxt.py
written
***Reading from a file***
{'m': 12, 'n': 23, 't': 34, 's': 45, 'u': 56}
****************
import pickle
f1= open("d:\\mfile3.txt", 'wb')
for i in range(0,10):
x=int(input('enter an integer:'))
pickle.dump(x,f1) #writing content on the file
f1.close()
Content in the binary mode
For loop for read file line by line
Write( ) function
Python allows you to open text files and read the content either all at
once, or line-by-line.
CSV is a module built-in to Python which will simplify working with
CSV files.
We need to import CSV module to work with csv files.
import csv
Opening a csv file:
<file object> = open(<name of csv file>)
# Write data on a file
fin=open("D:\\firstfile.txt",'r')
f2=open("d:\\secondfile.txt",'w')
s=fin.read(str)
f2.write(s)
print(s)
fin.close( )
Reader a csv file
csv.reader()
Reader function in csv module is used to read the contents of the files. content in the file are parsed and read as one
line at a time.
The line in parsed as a list and each line is treated as a list element. So, it may be thought of this way that each the
object in which data is read is a list of lists.
For example :
C=csv.reader(<file object>)
Str=f.read()
In above example, c is an object that is a list of lists.
To access the content from the object containing the data read from
csv file, a for loop should be used as follows :
for r in c :
print (r)
import csv
f = open('E:\\TLIST.csv')
csv_f = csv.reader(f)
for row in csv_f:
print (row)
Reading CSV file and displaying only 1st and
2nd column of the row
import csv
f = open('E:\\TLIST.csv')
csv_f = csv.reader(f)
for row in csv_f:
print (row[1], "\t",row[2])
Displaying only 10 records at a time
import csv
f = open('E:\\TLIST.csv')
csv_f = csv.reader(f)
c=0
for row in csv_f:
print (row[1], "\t",row[2])
c=c+1
if c==10:
x=input()
c=0
Reading a text file using a csv reader function
import csv
f = open('E:\\results\\650482016.txt')
csv_f = csv.reader(f)
c=0
for row in csv_f:
print (row)
Creating separate lists from the data read
For example : let us assume that a file has user type, name and its email address.
import csv
f = open('E:\\tlist.csv') The example
csv_f = csv.reader(f)
namelist=[]
creates a list
for r in csv_f: having names of
namelist.append(r[1])
teachers
print (namelist)
writer( ) : Writing in a CSV File .
Converting a list into a set helps in comparing
two lists
Set is a datatype that contains unique values :
import csv for row in csv_f:
f = open('attendees1.csv') attendee_emails2.append(row[2])
csv_f = csv.reader(f)
attendee_emails1 = [] attendee_emails1 = set(attendee_emails1)
for row in csv_f: attendee_emails2 = set(attendee_emails2)
attendee_emails1.append(row[2])
f = open('attendees2.csv')
csv_f = csv.reader(f)
attendee_emails2 = []
Finding the difference between the 2 sets
https://www.protechtraining.com/training/search?q=python
Connecting sql with python
C:\Users\admin\AppData\Local\Programs\
Python\Python37-32\Scripts>python -m pip
install mysql-connector-python
Download and installation of MySQL driver
complete
Test installation of my sql connector
import mysql.connector
create an object of database for connectivity
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword“ )
print(mydb)
Output :
>>>
RESTART: C:/Users/admin/AppData/Local/Programs/Python/Python37-32/sqltablaeempl.py
<mysql.connector.connection.MySQLConnection object at 0x024B02D0>
>>>
querying the database using SQL statements
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword“
database="mydatabase"
)
print(mydb)
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase“ )
mycursor = mydb.cursor()
mydb.commit()
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mydb.commit()
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Using the fetchone() Method
If you are only interested in one row, you can use the fetchone() method.
The fetchone() method will return the first row of the result:
Example
Fetch only one row:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
myresult = mycursor.fetchone()
print(myresult)
Select With a Filter
When selecting records from a table, you can
filter the selection by using the "WHERE"
statement:
Select record(s) where the address is "Park Lane 38": result:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Wildcard Characters
You can also select the records that starts, includes, or ends with a given letter or phrase.
Use the % to represent wildcard characters:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Escape query values by using the placholder
%s method:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute(sql, adr)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Sort the Result
Use the ORDER BY statement to sort the result in ascending or descending order.
The ORDER BY keyword sorts the result ascending by default. To sort the result in descending order, use the
DESC keyword.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
ORDER BY DESC
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Python MySQL Delete From By
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute(sql)
mydb.commit()
This is to prevent SQL injections, which is a common web hacking technique to destroy or misuse your database.
The mysql.connector module uses the placeholder %s to escape values in the delete statement:
Deleting records
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute(sql, adr)
mydb.commit()
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute(sql)
Drop Only if Exist
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute(sql)
Python MySQL Update Table
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "UPDATE customers SET address = 'Canyon 123' WHERE address = 'Valley 345'"
mycursor.execute(sql)
mydb.commit()
Python MySQL Limit
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Start From Another Position : If you want to return five records,
starting from the third record, you can use the "OFFSET"
keyword:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
myresult = mycursor.fetchall()
for x in myresult:
print(x)
Python MySQL Join
Join Two or More Tables
You can combine rows from two or more tables, based on a related column between them, by using a JOIN
statement.
Consider you have a "users" table and a "products" table:
users
{ id: 1, name: 'John', fav: 154},
{ id: 2, name: 'Peter', fav: 154},
These two tables can be
{ id: 3, name: 'Amy', fav: 155}, combined by using
{ id: 4, name: 'Hannah', fav:},
{ id: 5, name: 'Michael', fav:}
users' fav field and
products products' id field.
{ id: 154, name: 'Chocolate Heaven' },
{ id: 155, name: 'Tasty Lemons' },
{ id: 156, name: 'Vanilla Dreams' }
Join users and products to see the name of the users favorite
product:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
mycursor = mydb.cursor()
sql = "SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
INNER JOIN products ON users.fav = products.id"
mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
LEFT JOIN : In the example above, Hannah, and Michael were excluded from the result, that is
because INNER JOIN only shows the records where there is a match.
If you want to show all users, even if they do not have a favorite product, use the LEFT JOIN
statement:
sql = "SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
LEFT JOIN products ON users.fav = products.id"
RIGHT JOIN
If you want to return all products, and the users who have them as their
favorite, even if no user have them as their favorite, use the RIGHT JOIN
statement:
sql= "SELECT \
users.name AS user, \
products.name AS favorite \
FROM users \
RIGHT JOIN products ON users.fav = products.id"