文本文件中存储数据,最简单的是系列逗号分隔的CSV文件。分享一个气象数据csv文件

链接:https://pan.baidu.com/s/1lIsySGvjsoONGDLyaR39TQ 密码:5on4

处理CSV文件-七月最高最低气温趋势图

import csv
from datetime import datetime
from matplotlib import  pyplot as plt
filename='sitka_weather_2014.csv'
with open(filename) as f:
    #结果文件对象存在f中.创建与文件关联的阅读器对象。Reader处理第一行的数据
    reader=csv.reader(f)
    #返回文件每一列的表头
    header_row=next(reader)
    #enumerate获取每个元素的索引和值
    for index,column_header in enumerate(header_row):
        print(index,column_header)

    #最高气温是第一列
    #创建了三个空列表
    dates,highs,lows=[],[],[]
    for row in reader:
        try:     #对数值缺失导致错误的进行异常处理
            #时间是第0列,最低气温在第三列
            current_date=datetime.strptime(row[0],'%Y-%m-%d')
            high=int(row[1])
            low=int(row[3])
        except:
            print(current_date, "missing data")
        else:
            dates.append(current_date)
            highs.append(high)
            lows.append(low)
    print(highs)

    #根据数据绘制图形
    fig=plt.figure(dpi=128,figsize=(10,6))
    #日期-最高气温,红线,透明度0.5
    plt.plot(dates,highs,c='red',alpha=0.5)
    #日期-最低气温,蓝线,透明度0.5
    plt.plot(dates,lows,c='blue',alpha=0.5)
    #两线之间的填充区域
    plt.fill_between(dates,highs,lows,facecolor='blue',alpha=0.1)
    #设置图形的格式
    plt.title("Dialy high temperatures-2014",fontsize=16)
    plt.xlabel('',fontsize=16)
    fig.autofmt_xdate()
    plt.ylabel('Temperature(F)',fontsize=16)
    plt.tick_params(axis='both',which='major',labelsize=16)
    plt.show()

最终展现

csv模块官方文档 python python csv模块下载_bc

处理JSON文件得到交易收盘价股市图 (JSON是列表+字典)

对JSON的处理实际上是处理一个长Python列表,列表中的每个元素都是包含五个键的字典

下载收盘数据存在本地

from __future__ import (absolute_import,division,print_function,unicode_literals)
try:
    from urllib2 import urlopen
except:
    from urllib.request import urlopen
import json

json_url='https://raw.githubusercontent.com/muxuezi/btc/master/btc_close_2017.json'
response=urlopen(json_url)
#读取数据
req=response.read()
#数据写入文件
with open('btc_close_2017.json','wb') as f:
    f.write(req)
#加载JSON数据
fill_urlib=json.loads(req)
print(fill_urlib)

现在该url的JSON数据都在btc_close_2017.json文件中

提取相关数据

filename = 'btc_close_2017.json'
with open(filename, 'r') as fp:
    bct_data = json.load(fp)
for bct_dict in bct_data:
    date = bct_dict['date']
    month = bct_dict['month']
    week = bct_dict['week']
    weekday = bct_dict['weekday']
    close = bct_dict['close']
    print("{} is month {} week {},{}, the close price is {} RMB".format(date, month,week, weekday, close))

dates, months, weeks, weekdays, close = [], [], [], [], []
for bct_dict in bct_data:
    dates.append(bct_dict['date'])
    months.append(int(bct_dict['month']))
    weeks.append(int(bct_dict['week']))
    weekdays.append(bct_dict['weekday'])
    close.append(int(float(bct_dict['close'])))

########################上面将JSON中的所有数据放在了五个列表中##########################

import pygal
import math

line_chart = pygal.Line(x_label_rotation=20, show_minor_x_labels=False) # x轴的标签旋转20度  不全展示X轴的所有坐标
line_chart.title = '收盘价(¥)'
line_chart.x_labels = dates # x轴用dates作为标签
N = 20 # x轴坐标每20天显示一次,这个N传到了下面
line_chart.x_labels_major = dates[::N]
#对收盘价log处理
close_log = [math.log10(x) for x in close]

line_chart.add('log收盘价', close_log)
line_chart.render_to_file('收盘价对数变换折线图.svg')

###############################上面绘制了收盘价变换折线图##########################

from itertools import groupby

def draw_line(x_data, y_data, title, y_legend):
    xy_map = []
    #groupby函数分组
    for x, y in groupby(sorted(zip(x_data, y_data)), key=lambda _: _[0]):  # 2
        y_list = [v for _, v in y]
        #每组的均值
        xy_map.append([x, sum(y_list) / len(y_list)])  # 3
    x_unique, y_mean = [*zip(*xy_map)]  # 4
    line_chart = pygal.Line()
    line_chart.title = title
    line_chart.x_labels = x_unique
    line_chart.add(y_legend, y_mean)
    line_chart.render_to_file(title + '.svg')
    return line_chart

#2017-12-01是统计的截至日期
idx_month = dates.index('2017-12-01')
print(idx_month)
line_chart_month = draw_line(months[:idx_month], close[:idx_month], '收盘价月日均值', '月日均值')
idx_week = dates.index('2017-12-01')
print(idx_week)
line_chart_week = draw_line(weeks[1:idx_week], close[1:idx_week], '收盘价周日均值', '周日均值')

######################################################################################


idx_week = dates.index('2017-12-11')
wd = ['Monday', 'Tuesday', 'Wednesday',
      'Thursday', 'Friday', 'Saturday', 'Sunday']
weekdays_int = [wd.index(w) + 1 for w in weekdays[1:idx_week]]
#print(weekdays_int)
line_chart_weekday = draw_line(
    weekdays_int, close[1:idx_week], '收盘价星期均值(¥)', '星期均值')
line_chart_weekday.x_labels = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
line_chart_weekday.render_to_file('收盘价星期均值(¥)-final.svg')

#######################接下来是制作仪表盘,将前面图整合######################
with open('收盘价DASHBOARD.html', 'w', encoding='utf8') as html_file:
    html_file.write('<html><head><title>收盘价dashboard</title><matecharset="utf8"></head><body>\n')
    for svg in ['收盘价对数变换折线图.svg','收盘价月日均值.svg','收盘价周日均值.svg',
                '收盘价星期均值(¥).svg','收盘价星期均值(¥)-final.svg']:
        html_file.write('   <object type="image/svg+xml" data="{0}" height=500></object>\n'.format(svg))
    html_file.write('</body></html>')

对数折线图如图所示

csv模块官方文档 python python csv模块下载_json_02

csv模块官方文档 python python csv模块下载_数据_03

csv模块官方文档 python python csv模块下载_csv模块官方文档 python_04

csv模块官方文档 python python csv模块下载_数据_05