Python 强化训练:第九篇

主题

数据处理

  1. csv文件
  2. json文件
  3. xml: xpath
  4. excel

1.

  1. CSV: 逗号分隔值,其文件以纯文本形式存储表格数据(数字和文本)。
  2. 模块:csv
  3. 方法:csv.reader(), csv.writer(), csv.Dictreader(), csv.writerow(), csv.writerows()
import csv
headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
rows = [('AA', 39.48, '6/11/2007', '9:36am', -0.18, 181800),
         ('AIG', 71.38, '6/11/2007', '9:36am', -0.15, 195500),
         ('AXP', 62.58, '6/11/2007', '9:36am', -0.46, 935000),
       ]

with open('name.csv', newline="") as f:
    f_csv = csv.reader(f)
    headers = next(f_csv)
    print(headers)
    print("=====")
    for row in f_csv:
        print(row)
        print("===")


写入文件形式:

1478869402821.png

要求:将name.csv文件中Volume的值大于195500的数据写入name_copy.csv文件中.

import codecs
import csv

with codecs.open("name_copy.csv", 'w') as f_name_copy:
    f_name_one = csv.writer(f_name_copy)
    with codecs.open("name.csv", 'r') as f_name:
        f_name_two = csv.reader(f_name)
        headers = next(f_name_two)
        f_name_one.writerow(headers)
        for one in f_name_two:
            print(one)
            if int(one[5]) > 195500:
                f_name_one.writerow(one)

文件显示:


1478869756196.png

要求:获取雅虎指定股票历史数据,并存入csv文件中.

import requests
import csv
import codecs

response = requests.get('http://table.finance.yahoo.com/table.csv?s=000001.sz')
content = response.text

with codecs.open("pingan.csv", 'w') as f:
    content_all = csv.writer(f)
    for one in content.split('\n'):
        content_all.writerow(one.split(','))

Paste_Image.png

2.

python 如何处理json文件:

  1. json 模块
  2. dumps(),dump(), loads(),load()方法

import json
import codecs
# json.dumps()
# json.loads()
# json.dump()  # 接口是一个文件
# json.load()  # 接口是一个文件

one = {"wuhan": 10, "beijing": 1, "changsha": 6}
two = [1, 2, "apple", 'chuizi', {"a": 1, "b": 2}]
one_json = json.dumps(one, separators=[",  ", ":  "], indent=4)
one_1_json = json.dumps(one, sort_keys=True)
two_json = json.dumps(two, separators=[",", ":"])
print(one_json)
print(one_1_json)
print(two_json)

with codecs.open("one.json", 'w') as f:
     json.dump(one, f)

with codecs.open("one.json", 'r') as f:
    print(json.load(f))


转换对照表:

python json
dict object
list,tuple array
str,unicode string
int,long,float number
True true
False false
None null
print(json.dumps(None))
print(json.dumps(True))
print(json.dumps(False))

print(json.loads("null"))
print(json.loads("true"))
print(json.loads("false"))

# with codecs.open("one.json", 'w') as f:
#     json.dump(one, f)

with codecs.open("one.json", 'r') as f:
    print(json.load(f))


res = requests.get("http://www.weather.com.cn/data/cityinfo/101010100.html")
with codecs.open("weather.json", 'w', encoding="utf8") as f_wea:
    json.dump(res.text, f_wea)

with codecs.open("weather.json", 'r') as f_wea_r:
    A = json.load(f_wea_r)

print(A)


3.

xpath语法:

Syntax Meaning
tag Selects all child elements with the given tag.
* Selects all child elements.
. Selects the current node.
// Selects all subelements, on all levels beneath the current element.
.. Selects the parent element.
[@atrrib] Selects all elements that have the given attribute.
[@atrrib='value'] Selects all elements for which the given attribute has the given value.
[tag] Selects all elements that have a child named tag.
[tag="text"] Selects all elements that have a child named tag whose complete text content, including descendants, equals the given text.
[position] Selects all elements that are located at the given position.
from xml.etree.ElementTree import parse
import requests
import codecs
tree = parse("html.xml")
root = tree.getroot()
print(root.tag)
print(root.attrib)
for child in root:
    print(child.tag, child.attrib)

# tag: 查找给定标签的子节点
print(root.findall('country'))

# *:查找所有子节点
print(root.findall("*"))

# . : 查找当前节点
print(root.findall("."))

# // :所有子孙节点
print(root.findall('.//'))

# .. : 父节点
print(root.findall('.//rank/..'))

# [@atrrib] :带有这个属性值的元素
print(root.findall('.//country[@name]'))

# [@atrrib=“value”]
print(root.findall('.//country[@name="Liechtenstein"]'))

# [tag] : 带有tag子节点的节点
print(root.findall('[country]'))

4.

  1. 模块: xlrd, xlwt
  2. 功能: 负责读写操作

book.xlsx文件内容和结构:

1478938867731.png
import xlrd
import xlwt
name = xlrd.open_workbook('book.xlsx')
sheet = name.sheets()
for one in sheet:
    print(one.name)
result = name.sheet_by_name('result')
print(result.nrows, result.ncols)
one_one = result.cell(0, 0)
one_two = result.cell(0, 1)
one_three = result.cell(0, 2)
one_four = result.cell(0, 3)

# 1: text  2: number
print(one_one.ctype, one_one.value)
print(one_two.ctype, one_two.value)
print(one_three.ctype, one_three.value)
print(one_four.ctype, one_four.value)

print(result.row(1))
print(result.row_values(1))
print(result.row_values(1, 1))
print(result.col(1))
print(result.col_values(1))
print(result.col_values(1, 1))

result.put_cell(0, result.ncols, xlrd.XL_CELL_TEXT, u"总分", None)
for row in range(1, result.nrows):
    t = sum(result.row_values(row, 1))
    print(t)
    result.put_cell(row, result.ncols, xlrd.XL_CELL_NUMBER, t, None)

wbook = xlwt.Workbook()
wsheet = wbook.add_sheet("Sheet1")

style = xlwt.easyxf("align: vertical center, horizontal center")
value = [["名称", "hadoop编程实战", "hbase编程实战", "lucene编程实战"], ["价格", "52.3", "45", "36"], ["出版社", "机械工业出版社", "人民邮电出版社", "华夏人民出版社"], ["中文版式", "中", "英", "英"]]
for i in range(0, 4):
    for j in range(0, len(value[i])):
        wsheet.write(i, j, value[i][j], style)
wbook.save("wbook1.xls")

friend = name.sheet_by_index(1)
friend_copy = name.sheet_by_name("friend")

print(friend.nrows, friend.ncols)
print(friend_copy.nrows, friend_copy.ncols)



最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,825评论 4 377
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,887评论 2 308
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 112,425评论 0 255
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,801评论 0 224
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,252评论 3 299
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,089评论 1 226
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,216评论 2 322
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 31,005评论 0 215
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,747评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,883评论 2 255
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,354评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,694评论 3 265
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,406评论 3 246
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,222评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,996评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,242评论 2 287
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 36,017评论 2 281

推荐阅读更多精彩内容