Created
October 30, 2018 02:15
-
-
Save sin2akshay/b7ff5f49a15b7c91883a310fcb659f63 to your computer and use it in GitHub Desktop.
Python Summary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####CSV#### | |
> Reading CSV | |
from datetime import datetime | |
import csv | |
with open(filename, 'rb') as csvfile: | |
monthlyBill = csv.reader(csvfile, delimiter = ',', quotechar='|') | |
for i, row in enumerate(monthlyBill, start = 0): | |
if(i > 0): | |
du = DataUsage(row[0],row[1]+" "+row[2],row[3],row[4]); | |
dataUsageList.append(du); | |
####URL#### | |
> Reading from a url | |
import urllib2 | |
url = 'www.abc.com/xyz.xml' | |
request = urllib2(url, headers = {"Accept":"application/xml"}) | |
u = urllib2.urlopen(request) | |
####XML#### | |
> Reading an XML | |
import xml.etree.ElementTree as ET | |
tree = ET.parse(u) #That we got from URL above | |
rootElem = tree.getroot() | |
> Parsing an XML | |
root = rootElem #We got from Reading XML in previous step | |
callList = [] | |
i = 0; | |
for child in root: | |
mb = CallDetails() | |
mb.setCustomerId(child.find('customerid').text) | |
mb.setMobileNumber(child.find('tonumber').text) | |
mb.setDuration(child.find('duration').text) | |
mb.setAmount(child.find('amount').text) | |
callList.append(mb) | |
#Another Use | |
dataUsageList = [] | |
i = 0; | |
for child in root: | |
limit = None; | |
cusId = child.attrib.get('id') | |
for ch in child.getchildren(): | |
if ch.tag == "usage": | |
du = DataUsage(cusId,limit,ch.find("date").text,ch.find("pulse").text) | |
dataUsageList.append(du) | |
else : | |
limit = ch.text | |
####DataBase#### | |
> Reading data from DB | |
import MySQLdb | |
db = self.connectToDB(); | |
cursor = db.cursor(); | |
sql = """select * from call_details;""" | |
c = None | |
try: | |
cursor.execute(sql); | |
data = cursor.fetchall() | |
for r in data: | |
c = CallDetails(r[0],r[1], r[2], r[3]); | |
callList.append(c) | |
except Exception, e: | |
print "Unable to fetch data ",e; | |
> Writing data to DB | |
db = self.connectToDB() | |
cursor = db.cursor() | |
for r in callList: | |
sql = """INSERT INTO call_details(customer_id,mobile_number,duration,amount) VALUES ('"""+r.getCustomerId()+"""','"""+r.getMobileNumber()+"""','"""+r.getDuration()+"""','"""+r.getAmount()+"""');""" | |
try: | |
cursor.execute(sql); | |
db.commit(); | |
except Exception, e: | |
print "Error to insert a data ",e; | |
db.rollback(); | |
db.close(); | |
####Regular Expression#### | |
> Checking a string against a RE | |
import re | |
if re.match('((?=.*\\d).{10,})', pno): #Phone No. | |
return 1 | |
if(re.match('[^@]+@[^@]+\.[^@]+', email)): #Email | |
return 1 | |
if re.match('((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,})', password): #Password | |
return 1 | |
#Minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character: | |
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$" | |
####DateTime Conversion#### | |
> Using strptime | |
from datetime import datetime | |
datetime_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p') | |
> Using strftime | |
import time | |
t = (2009, 2, 17, 17, 3, 38, 1, 48, 0) | |
t = time.mktime(t) | |
print time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment