Coursera 零基础Python入门系列课程习题解答

Chapter7 开始的答案,兹以广大网友参考

1、files读取本地文件

fname = raw_input("Enter file name: ")
fh = open(fname)
for line in fh:
    print line.strip().upper()

2、files读取本地文件

fname = raw_input("Enter file name: ")
fh = open(fname)
add = 0
count=0
for line in fh:
    if not line.startswith("X-DSPAM-Confidence:") : 
        continue
    num = line.find(":")
    add = add+float(line[num+1:])
    count=count+1        
print "Average spam confidence:",add/count

3、lists可变列表

fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list()
words = list()
for line in fh:
    words = line.split()
    for word in words:
    if lst.count(word)==0:
            lst.append(word)
lst.sort()
print lst

4、lists可变列表

fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
lst =list()
for line in fh:
    if line.startswith("From "):
        lst = line.split()
        print lst[1]
        count=count+1
print "There were", count, "lines in the file with From as the first word"

5、dict字典

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
words=list()
icount=dict()
for line in handle:
    if line.startswith("From "):
        words=line.split()
        word=words[1]
        icount[word]=icount.get(word,0)+1
maxkey=""
maxvalue=0
for ikey,ivalue in icount.items():
    if ivalue > maxvalue:
        maxkey=ikey
        maxvalue=ivalue
print maxkey,maxvalue

6、dict字典

name = raw_input("Enter file:")
if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
icount=dict()
ilist=list()
for line in handle:
    if line.startswith("From "):
        words=line.split()
        word=words[5]
        words=word.split(":")
        word=words[0]
        icount[word]=icount.get(word,0)+1
ilist=icount.keys()
ilist.sort()
for sth in ilist:
    print sth,icount[sth]

7、re正则表达式

import re
handle = open("regex_sum_341348.txt")
lines=handle.read()
y = re.findall('[0-9]+', lines)
sum=0
for word in y:
    sum=int(word)+sum
print sum

8、urllib网页读取

import urllib
fhand=urllib.urlopen('http:...')
for line in fhand:
    print line.strip()

9、urllib网页读取

import urllib
from BeautifulSoup import *
html=urllib.urlopen('http:...').read()
soup = BeautifulSoup(html)
tags=soup('a')
for tag in tags
   print 'TAG:',tag
   print 'URL:',tag.get('href', None)
   print 'Contents:',tag.contents[0]
   print 'Attrs:',tag.attrs

10、urllib网页读取

import urllib
from BeautifulSoup import *
html=urllib.urlopen('http://python-data.dr-chuck.net/comments_341353.html').read()
soup = BeautifulSoup(html)
tags=soup('span')
sum=0
for tag in tags:
    sum=sum+int(tag.contents[0])
print sum

11、urllib网页读取

import urllib
from BeautifulSoup import *
url = raw_input('Enter - ')
for i in range(1,8):
    html = urllib.urlopen(url).read()
    soup = BeautifulSoup(html)
    # Retrieve all of the anchor tags
    tags = soup('a')
    count=1
    for tag in tags:
        url = tag.get('href', None)
        print url
        if count==18 :
            print 'Next'
            break
        count=count+1

12.xml文件读取

参考:http://www.pythonlearn.com/code/geoxml.py

import urllib
import xml.etree.ElementTree as ET
url = 'http://python-data.dr-chuck.net/comments_341350.xml'
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
tree = ET.fromstring(data)
results = tree.findall('comments/comment')
icount=len(results)
isum=0
for result in results:
    isum += float(result.find('count').text)
print 'Count:',icount
print 'Sum:',isum

13.json文件读取

import urllib
import json
url = 'http://python-data.dr-chuck.net/comments_341354.json'
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
info = json.loads(data)
isum=0
for item in info["comments"]:
    isum += item["count"]
print 'Count:',len(info["comments"])
print 'Sum:',isum

14、json文件读取

import urllib
import json
serviceurl = 'http://python-data.dr-chuck.net/geojson?'
while True:
    address = raw_input('Enter location: ')
    if len(address) < 1 : break
    url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})
    print 'Retrieving', url
    uh = urllib.urlopen(url)
    data = uh.read()
    print 'Retrieved',len(data),'characters'
    try: js = json.loads(str(data))
    except: js = None
    if 'status' not in js or js['status'] != 'OK':
        print '==== Failure To Retrieve ===='
        print data
        continue
    print json.dumps(js, indent=4)
    plid = js["results"][0]["place_id"]
    print 'place_id:',plid

15、sqlite数据库操作

import sqlite3
conn = sqlite3.connect('emaildb.sqlite')
cur = conn.cursor()
cur.execute('''
DROP TABLE IF EXISTS Counts''')
cur.execute('''
CREATE TABLE Counts (org TEXT, count INTEGER)''')
fname = raw_input('Enter file name: ')
if ( len(fname) < 1 ) : fname = 'mbox.txt'
fh = open(fname)
for line in fh:
    if not line.startswith('From: ') : continue
    pieces = line.split()
    pieces=pieces[1].split("@")
    org = pieces[1]
    cur.execute('SELECT count FROM Counts WHERE org = ? ', (org, ))
    row = cur.fetchone()
    if row is None:
        cur.execute('''INSERT INTO Counts (org, count) 
                VALUES ( ?, 1 )''', ( org, ) )
    else : 
        cur.execute('UPDATE Counts SET count=count+1 WHERE org = ?', 
            (org, ))
conn.commit()
sqlstr = 'SELECT org, count FROM Counts ORDER BY count DESC LIMIT 10'
print "Counts:"
for row in cur.execute(sqlstr) :
    print str(row[0]), row[1]
cur.close()

16、sqlite3数据库操作

import xml.etree.ElementTree as ET
import sqlite3

conn = sqlite3.connect('trackdb.sqlite')
cur = conn.cursor()

cur.executescript('''
DROP TABLE IF EXISTS Artist;
DROP TABLE IF EXISTS Album;
DROP TABLE IF EXISTS Track;
DROP TABLE IF EXISTS Genre;

CREATE TABLE Artist (
    id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name    TEXT UNIQUE
);

CREATE TABLE Genre (
    id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name    TEXT UNIQUE
);                   

CREATE TABLE Album (
    id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    artist_id  INTEGER,
    title   TEXT UNIQUE
);

CREATE TABLE Track (
    id  INTEGER NOT NULL PRIMARY KEY 
        AUTOINCREMENT UNIQUE,
    title TEXT  UNIQUE,
    album_id  INTEGER,
    genre_id  INTEGER,    
    len INTEGER, rating INTEGER, count INTEGER
);
''')

fname = raw_input('Enter file name: ')
if ( len(fname) < 1 ) : fname = 'Library.xml'

def lookup(d, key):
    found = False
    for child in d:
        if found : return child.text
        if child.tag == 'key' and child.text == key :
            found = True
    return None

stuff = ET.parse(fname)
all = stuff.findall('dict/dict/dict')
print 'Dict count:', len(all)
for entry in all:
    if ( lookup(entry, 'Track ID') is None ) : continue

    name = lookup(entry, 'Name')
    artist = lookup(entry, 'Artist')
    genre = lookup(entry,'Genre')
    album = lookup(entry, 'Album')
    count = lookup(entry, 'Play Count')
    rating = lookup(entry, 'Rating')
    length = lookup(entry, 'Total Time')

    if name is None or artist is None or album is None or genre is None : 
        continue

    print name, artist, genre, album, count, rating, length

    cur.execute('''INSERT OR IGNORE INTO Artist (name) 
        VALUES ( ? )''', ( artist, ) )
    cur.execute('SELECT id FROM Artist WHERE name = ? ', (artist, ))
    artist_id = cur.fetchone()[0]

    cur.execute('''INSERT OR IGNORE INTO Genre (name) 
        VALUES ( ? )''', ( genre, ) )
    cur.execute('SELECT id FROM Genre WHERE name = ? ', (genre, ))
    genre_id = cur.fetchone()[0]

    cur.execute('''INSERT OR IGNORE INTO Album (title, artist_id) 
        VALUES ( ?, ? )''', ( album, artist_id ) )
    cur.execute('SELECT id FROM Album WHERE title = ? ', (album, ))
    album_id = cur.fetchone()[0]

    cur.execute('''INSERT OR REPLACE INTO Track
        (title, album_id, genre_id, len, rating, count) 
        VALUES ( ?, ?, ?, ?, ?, ? )''', 
        ( name, album_id, genre_id, length, rating, count ) )

    conn.commit()

17、sqlite3 (many to many)

import json
import sqlite3

conn = sqlite3.connect('rosterdb.sqlite')
cur = conn.cursor()

cur.executescript('''
DROP TABLE IF EXISTS User;
DROP TABLE IF EXISTS Member;
DROP TABLE IF EXISTS Course;

CREATE TABLE User (
    id     INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name   TEXT UNIQUE
);

CREATE TABLE Course (
    id     INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    title  TEXT UNIQUE
);

CREATE TABLE Member (
    user_id     INTEGER,
    course_id   INTEGER,
    role        INTEGER,
    PRIMARY KEY (user_id, course_id)
)
''')

fname = raw_input('Enter file name: ')
if ( len(fname) < 1 ) : fname = 'roster_data.json'

str_data = open(fname).read()
json_data = json.loads(str_data)

for entry in json_data:

    name = entry[0];
    title = entry[1];
    role = entry[2];

    print name, title, role

    cur.execute('''INSERT OR IGNORE INTO User (name) 
        VALUES ( ? )''', ( name, ) )
    cur.execute('SELECT id FROM User WHERE name = ? ', (name, ))
    user_id = cur.fetchone()[0]

    cur.execute('''INSERT OR IGNORE INTO Course (title) 
        VALUES ( ? )''', ( title, ) )
    cur.execute('SELECT id FROM Course WHERE title = ? ', (title, ))
    course_id = cur.fetchone()[0]

    cur.execute('''INSERT OR REPLACE INTO Member
        (user_id, course_id, role) VALUES ( ?, ?, ? )''', 
        ( user_id, course_id, role ) )

    conn.commit()
  • 3
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值