麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Notes of Py for informatics 2

2019-11-10 19:19:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Chapter 7 Files

File PRocessing

www.py4inf.com/code/mbox-short.txt

open() returns a "file handle" - a variable used to perform Operations on the file

handle is a connection. doesn't actually have data

kind of like "File -> Open" in a Word Processor

handle = open(filename, mode) #filename is a stringfhand = open('mbox.txt','r') #'r':reading,'w':writing

The newline Character

stuff = 'Hello/nWorld'  #/n means new line, and it is counted as a characterprint stuffprint len(stuff)output:
HelloWorld11
Counting Lines in a File
xfile = open('/Users/huyifan/documents/mbox.txt')count = 0for line in xfile:    count = count + 1print 'Line Count:',countimportant! A file handle ope for read can be treated as a sequence of strings where each line in the file is a string in the sequence
Reading the *Whole* File
Use read() to read the whole file (newlines and all) into a single string.
xfile = open('/Users/huyifan/documents/mbox.txt')inp = xfile.read()print len(inp)print inp[:20]output:
94625From stephen.marquarSearching Through a File
for line in xfile:    if line.startswith('Author'):        print lineoutput:
Author: [email protected]                                     #why there are black lines? because of /n !Author: [email protected]: [email protected]: [email protected]: [email protected]: [email protected] can use the rstrip() or lstrip().
for line in xfile:    if line.startswith('A'):        print line.rstrip()output:
Author: [email protected]: [email protected]: [email protected]: [email protected]: [email protected]: [email protected]: [email protected]: [email protected]
Skipping with continue
for line in xfile:    if not line.startswith('Author'):  #or: if not '@media.berkeley.edu' in line:        continue   #process our 'interesting lines'    print line.rstrip()The output will be the same.
Bad File Names
fname = raw_input('Enter the file name:')try:    fhand = open(fname)except:    print 'File cannot be openned:', fname    exit()count = 0for line in fhand:    if line.startswith('Author:'):        count = count + 1print 'There were', count, 'pieces of important information'

Chapter 8 Lists

8.1 basic understanding and operationsA list is a kind of collection.
A collection allows us to put many values in a single "variable"
like: 
friends = ['Joe','Joey','Joseph']
A list element can be any Python object - even another list
print [[1,2],3,4]
A list can be empty
print []Strings are "immutable" - we cannot change change the contents of a string - we must make a new string to make any change
Lists are "mutable" - we can change an element of a list using the index operator
len()
print len([[1,2],3,4])Using the range function: generate a list
print range(4)  # often be used as: for i in range(len(friends))    friend[i]output:
[0, 1, 2, 3]
Concatenating list using +
print range(4)+[2,3]output:
[0, 1, 2, 3, 2, 3]Slice lists: like 'strings'
8.2 Build-in Functions
Building a list from scratch: xxx.append()
stuff = list()stuff.append('book')stuff.append(99)print stuff

output:

['book', 99]xxx in xxxx?

Order the list

friends = ['Joey','Joe','Joseph','Mike','Ellen']friends.sort()print friendsoutput:

['Ellen', 'Joe', 'Joey', 'Joseph', 'Mike']   #important: it has changed itself!The sort method (unlike in strings) means "sort yourself"

other useful build-in functions

len(numbers), max(numbers), min(numbers), sum(numbers)Strings -> Lists: Use split()/split(';')/... to break a string into parts and produces a list of strings. (leave out all the comma/space..)

abc = 'With three words'stuff = abc.split()print stuffprint len(stuff)output:
['With', 'three', 'words']3
abc = 'With;three;words'stuff1 = abc.split()stuff2 = abc.split(';')   #important!print stuff1print stuff2print len(stuff1)print len(stuff2)output:
['With;three;words']['With', 'three', 'words']13


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 免费高潮在线国 | 成人不卡在线观看 | 91久久国产综合久久91猫猫 | 深夜精品福利 | 欧美日韩一区二区综合 | 免费视频aaa| 经典三级在线视频 | 亚洲骚综合 | 怦然心动50免费完整版 | 久章草在线视频 | 国产精品久久久久网站 | 免费毛片电影 | 九色免费视频 | 在线中文日韩 | 精品国产一区二区三区在线 | 国产亚洲精品精 | 日韩字幕在线 | 亚洲第一成av人网站懂色 | 欧美成人精品欧美一级乱黄 | 91aa.app| 91久久久久久亚洲精品禁果 | 亚洲影视在线 | 亚洲精品一区中文字幕 | 精品国产一区二区三区久久久 | 成人在线视频在线观看 | 超碰97人 | 黄色毛片免费视频 | 欧美在线成人影院 | 欧美黄色一级带 | 草草视频免费 | 日本在线播放一区二区 | 九九综合视频 | 久久99精品久久久久久久久久久久 | 全黄性性激高免费视频 | 97久久精品一区二区三区观看 | 国产福利不卡一区二区三区 | 大胆在线日本aⅴ免费视频 美国黄色毛片女人性生活片 | 欧美性生活视频免费看 | free台湾极品性hd | 一级外国毛片 | 全黄毛片|