Python Coursera 1
Python Coursera 1
LOCAL DATA
RETRIEVAL
Nanjing University
Data Retrieval with Python 3
• Read files
Write files
Nanjing University
Open File 4
S ource
>>> f1 = open('d:\\infile.txt')
>>> f2 = open(r'd:\outfile.txt', 'w')
>>> f3 = open('record.dat', 'wb', 0)
file_obj = open(filename, mode='r', buffering=-1, …)
• mode is an optional parameter with default value ‘r’
• buffering is an optional integer used to set the buffering policy. Pass 0
to switch buffering off (only allowed in binary mode), 1 to select line
buffering (only usable in text mode), and an integer > 1 to indicate the
size in bytes of a fixed-size chunk buffer.
Nanjing University
open() -mode 5
Mode Function
b binary mode
Nanjing University
File Related Function 6
Return Value
• open() returns a file object
• File object is iterative
• There exists functions/methods to read/write/close files.
– f.read(), f.write(), f.readline(), f.readlines(), f.writelines()
– f.close()
– f.seek()
Nanjing University
Write a File-f.write() 7
• file_obj.write(str)
− Write a string into file
S ource
firstpro.txt :
>>> f = open('firstpro.txt', 'w') Hello, World!
>>> f.write('Hello, World!')
>>> f.close()
S ource
• file_obj.read(size)
− Read at most size byte of data from file, return a string.
• file_obj.read()
− Read file till the end, return a string
S ource
Nanjing University
9
Other Read/Write Functions
F ile
Output:
['GOOGLE Inc.\n', 'Microsoft Corporation\n', 'Apple Inc.\n',
'Facebook, Inc.']
Nanjing University
10
Example
Add sequence number 1, 2, 3, … to the strings in file
companies.txt, and write into another file scompanies.txt.
F ile
Nanjing University
11
Other File Related Functions
S ource
INTERNET DATA
RETRIVAL
Nanjing University
Data Retrieval with Python 14
Nanjing University
Requests Library 15
• Requests library is a simple, easy and user-friendly Python HTTP third party library.
• Requests Official Site:http://www.python-requests.org/
• Basic method
request resource at given URL ,
requests.get()
corresponding to GET in HTTP.
Nanjing University
Dow Jones Constituent 16
http://finance.yahoo.com/q/cp?s=%5EDJI+Component
http://money.cnn.com/data/dow30/
Nanjing University
Get Doe Jones Constituent with Requests17
# Filename: dji.py
import requests
re = requests.get('http://money.cnn.com/data/dow30/') # the url may change
print(re.text)
Nanjing University
Interpreting Webpages 18
Nanjing University
19
SEQUENCE
Nanjing University
Sequence 20
Nanjing University
21
Strings
Lists
Tuples
Nanjing University
Sequence in Python 22
0 1 2 3 4 5 6
week 'Monday' 'Tuesday' 'Wednesday' 'Thursday' 'Friday' 'Saturday' 'Sunday'
-7 -6 -5 -4 -3 -2 -1
Sequence
Visit mode
0 1 2 N-2 N-1
• Elements are visited by
…
index offset from 0.
-N -(N-1) -(N-2) -2 -1
• One or multiple elements
can be visited at one time
Nanjing University
Sequence-Related Function 23
Nanjing University
24
Standard Operator
S ource
Nanjing University
25
Standard Operator
Nanjing University
Sequence Operator 26
S ource
Nanjing University
Sequence Operator 27
x in s
x not in s
s+t
s * n, n * s
s[i]
s[i:j]
s[i:j:k]
Nanjing University
Sequence Type Conversion 28
S ource
Nanjing University
Available Functions for Sequence 29
Nanjing University
30
STRING
Nanjing University
Different Formats of String 31
F ile
# Filename: puncount.py
aStr = "Hello, World!"
bStr = aStr[:7] + "Python!" Output:
count = 0 2
for ch in bStr[:]:
if ch in ',.!?':
count += 1
print(count)
Nanjing University
String and Output Format 33
Output:
Punctuation marks = 2
Output:
2
Output:
There are 2 punctuation marks.
print('There are %d punctuation marks. ' % (count))
format_string % (arguments_to_convert)
print('There are {0:d} punctuation marks. '.format(count))
format_string.format(arguments_to_convert)
Nanjing University
Type Specifier 34
Type Meaning
b Binary format. Outputs the number in base 2
o Octal format. Outputs the number in base 8
x Hex format. Outputs the number in base 16, using lower- case
letters for the digits above 9 (upper-case if use ‘X’)
c Character. Converts the integer to the corresponding unicode
character before printing.
d Decimal Integer. Outputs the number in base 10.
f Fixed point. Displays the number as a fixed-point number. The
default precision is 6.
e Exponent notation. Prints the number in scientific notation using
the letter ‘e’ to indicate the exponent. The default precision is 6.
Nanjing University
Other Available Format 35
符号 描述
+m.nf Output number with sign, keep n digits, and total length is m (if the
number is longer than m, then neglect the constraint)
< Forces the field to be left-aligned, default filling the right with spaces
0>5d Forces the field to be right-aligned, use 0 to fill left part, total length is 5
^ Forces the field to be centered within the available space.
{{}} Output {}
[Alignment][Sign][Minimum width][.Precision][Type]
>>> age, height = 21, 1.758
>>> print("Age:{0:<5d}, Height:{1:5.2f}".format(age, height))
Age:21 , Height: 1.76
Nanjing University
36
Use format() to Output Formatted String
S ource
Nanjing University
String Application 37
F ile F ile
Nanjing University
Useful Methods for String 38
Nanjing University
Application of String 39
# Filename: totitle.py
aStr = 'What do you think of this saying "No pain, No gain"?'
lindex = aStr.index('\"',0,len(aStr))
rindex = aStr.rindex('\"',0,len(aStr)) tempstr= aStr.split("\"")[1]
tempStr = aStr[lindex+1:rindex]
if tempStr.istitle():
print('It is title format.')
else:
print('It is not title format.')
print(tempStr.title())
Nanjing University
Escape Character 40
Character Meaning
\0 Empty Character \OOO Character with octal value ooo
\a ASCII Bell (BEL)
\b ASCII Backspace (BS) \xXX Character with hex value XX
\t ASCII Horizontal Tab
(TAB)
\n ASCII Linefeed (LF)
\v ASCII Vertical Tab (VT) S ource
LIST
Nanjing University
List 42
scalable S ource
Contain S ource
container different
object >>> aList = list('Hello.') types of >>> bList = [1, 2, 'a', 3.5]
>>> aList objects
['H', 'e', 'l', 'l', 'o', '.']
>>> aList = list('hello.')
>>> aList
['h', 'e', 'l', 'l', 'o', '.']
>>> aList[0] = 'H'
>>> aList
['H', 'e', 'l', 'l', 'o', '.']
Nanjing University
Format of List 43
• aList = [1, 2, 3, 4, 5]
• names = ['Zhao', 'Qian', 'Sun', 'Li']
• bList = [3, 2, 1, 'Action']
• pList = [('AXP', 'American Express Company', '78.51'),
('BA', 'The Boeing Company', '184.76'),
('CAT', 'Caterpillar Inc.', '96.39'),
('CSCO', 'Cisco Systems, Inc.', '33.71'),
('CVX', 'Chevron Corporation', '106.09')]
Nanjing University
List 44
F ile
One school holds a competition,
the rate of each singer is # Filename: scoring.py
jScores = [9, 9, 8.5, 10, 7, 8, 8, 9, 8, 10]
decided by 10 judges and aScore = 9
audience. The rule of rating is to jScores.sort()
remove the highest and lowest jScores.pop()
jScores.pop(0)
rating of 10 judges, and average jScores.append(aScore)
with the rate of audience. aveScore = sum(jScores)/len(jScores)
Judges: 9、9、8.5、10、7、8、8、 print(aveScore)
9、8 and 10, [7, 8, 8, 8, 8.5, 9, 9, 9, 10, 10]
Audience: 9 [8, 8, 8, 8.5, 9, 9, 9, 10]
Compute the final result. [8, 8, 8, 8.5, 9, 9, 9, 10, 9]
8.72222222222
Nanjing University
List 45
F ile Output:
# Filename: week.py 1 Monday
week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] 2 Tuesday
weekend = ['Saturday', 'Sunday'] 3 Wednesday
week.extend(weekend) 4 Thursday
for i, j in enumerate(week): 5 Friday
6 Saturday
print(i+1, j)
7 Sunday
Nanjing University
List Methods 46
append() Parameters
copy() list.sort(key=None, reverse=False)
count() S ource
Nanjing University
List Comprehension 47
Nanjing University
48
TUPLE
Nanjing University
Tuple 49
Nanjing University
Tuple 50
Nanjing University
Tuple 51
• Type of function
S ource S ource
Nanjing University
52
Application of Tuple
Where to use?
Nanjing University
Variable Length Position Parameter(Tuple)
53
S ource
Nanjing University
Tuple as a Return Type 55
S ource
Number of Return
return value(s) Type >>> def foo():
0 None return 1, 2, 3
1 object >>> foo()
>1 tuple (1, 2, 3)
Nanjing University
Summary 56
Nanjing University