Notes of Py for informatics 2

本文介绍了Python中文件操作的基础,包括打开、读取、遍历文件等,并详细解释了如何使用不同模式打开文件以及处理文件中的每一行。此外,还探讨了列表的基本概念和常用操作,如追加元素、排序等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 string
fhand = open('mbox.txt','r') #'r':reading,'w':writing


The newline Character

stuff = 'Hello\nWorld'  #\n means new line, and it is counted as a character
print stuff
print len(stuff)
output:
Hello
World
11

Counting Lines in a File
xfile = open('/Users/huyifan/documents/mbox.txt')
count = 0
for line in xfile:
    count = count + 1

print 'Line Count:',count
important! 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:
94625
From stephen.marquar

Searching Through a File
for line in xfile:
    if line.startswith('Author'):
        print line
output:
Author: stephen.marquard@uct.ac.za
                                     #why there are black lines? because of \n !
Author: louis@media.berkeley.edu

Author: zqian@umich.edu

Author: rjlowe@iupui.edu

Author: zqian@umich.edu

Author: rjlowe@iupui.edu
We can use the rstrip() or lstrip().
for line in xfile:
    if line.startswith('A'):
        print line.rstrip()

output:
Author: david.horwitz@uct.ac.za
Author: stephen.marquard@uct.ac.za
Author: louis@media.berkeley.edu
Author: louis@media.berkeley.edu
Author: ray@media.berkeley.edu
Author: cwen@iupui.edu
Author: cwen@iupui.edu
Author: cwen@iupui.edu
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 = 0
for line in fhand:
    if line.startswith('Author:'):
        count = count + 1
print 'There were', count, 'pieces of important information'


Chapter 8 Lists

8.1 basic understanding and operations
A 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 friends
output:

['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 stuff
print len(stuff)
output:
['With', 'three', 'words']
3

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

























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值