UNIT4
UNIT4
Output
['i', 'i', 'i']
Output
['i', 'i', ‘I’, 'i']
Output
The_rain_in_India
Output
The_rain_in India
[a-zA-Z] Returns a match for any character alphabetically between a and z, lower
case OR upper case
Prepared By : Radadiya Jitendra 20
Sets
Example
import re
str = "Welcome 2 the World of Python"
x = re.findall("[a-m]",str)
print(' '.join(x))
Output
elcmeheldfh
Output
Wo 2 t Wor o Pyton
Output
2
Output
Wel Wor
Prepared By : Radadiya Jitendra 27
Sets
Check if the string starts with 'Welcome‘
import re
str = "Welcome 2 the World of Python"
x = re.findall('^Wel', str)
if (x):
print("Yes, the string starts with 'Welcome'")
else:
print("No match")
Output
Yes, the string starts with 'Welcome'
Prepared By : Radadiya Jitendra 28
Sets
Check if the string contains "or" followed by 0 or more "x"
characters
import re
str = "Welcome 2 the World of Python"
x = re.findall("or*", str)
print(' '.join(x))
Output
o or o o
id,name,department,birthday month
1,Raj,BCA,November
2,Jay,BBA,March
3,Ajay,BCOM,June
Prepared By : Radadiya Jitendra 34
CSV Module
Example Reading CSV Files
import csv
with open('std_DB1.txt') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print("Column names are "," ".join(row))
line_count += 1
else:
print("\t",row[1]," study in the ",row[2]," department
and was born in ",row[3])
line_count += 1
print("Processed" ,line_count-1,"lines.")
Prepared By : Radadiya Jitendra 35
CSV Module
Output
Output :
{'name': ‘Raj', 'languages': ['English', ‘Hindi']}
Output :
File Write Successfully"
import json
person_dict = {'name': ‘Raj', 'age': 12, ‘stream’: ‘BCA’ }
person_json = json.dumps(person_dict)
print(person_json)
Output :
{"name": “Raj", "age": 12, “stream": “BCA”}
Output :
{'name': ‘Raj', 'languages': ['English', ‘Hindi']}
['English', ‘Hindi']
Here, you register callbacks for events of interest and then let
the parser proceed through the document.