Python数据分析与机器学习实战(2)处理txt,csv,xsl,json,xml格式数据

如果需要数据,评论区留下邮箱!

Python IO与档案处理

f = open('tmp.txt','w')
f.write('hello world')
f.close()

使用with语句省去close()操作

with open('tmp.txt','w') as f:
    f.write('hello\nworld')
with open('tmp.txt','r') as f:
    print(f.read())
hello
world

将文本一行一行的输出,Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。

with open('tmp.txt','r') as f:
    for line in f.readlines():
        print(line.strip())
hello
world

处理CSV,Excel格式数据

数据来源:国家数据近五年人口数

with open('data\population.csv','r')as f:
    for line in f.readlines():
        print(line)
指标,2017年,2016年,2015年,2014年,2013年

年末总人口(万人),139008,138271,137462,136782,136072

男性人口(万人),71137,70815,70414,70079,69728

女性人口(万人),67871,67456,67048,66703,66344

城镇人口(万人),81347,79298,77116,74916,73111

乡村人口(万人),57661,58973,60346,61866,62961

使用pandas库的dataframe结构能更好的操作这些数据

import pandas as pd
df = pd.read_csv('data\population.csv',encoding='ANSI')
df
指标2017年2016年2015年2014年2013年
0年末总人口(万人)139008138271137462136782136072
1男性人口(万人)7113770815704147007969728
2女性人口(万人)6787167456670486670366344
3城镇人口(万人)8134779298771167491673111
4乡村人口(万人)5766158973603466186662961
import pandas as pd
df = pd.read_excel('data\population.xls',encoding='ANSI')
df
指标2017年2016年2015年2014年2013年
0年末总人口(万人)139008138271137462136782136072
1男性人口(万人)7113770815704147007969728
2女性人口(万人)6787167456670486670366344
3城镇人口(万人)8134779298771167491673111
4乡村人口(万人)5766158973603466186662961

处理json格式数据

数据来源城市信息

with open('data\city.json','r',encoding='utf-8') as f:
    city=f.read()

从json格式转换成Python中的字典

import json
dic = json.loads(city)
dic
[{'Code': '414500',
  'Name': '郑州市',
  'level': [{'Code': '414500', 'Name': '二七区', 'sort': 1},
   {'Code': '414500', 'Name': '中原区', 'sort': 2}],
  'sort': 1},
 {'Code': '414530',
  'Name': '新乡市',
  'level': [{'Code': '414530', 'Name': '卫滨区', 'sort': 1},
   {'Code': '414530', 'Name': '牧野区', 'sort': 2}],
  'sort': 2}]
for city in dic:
    print(city.get('Code'),city.get('Name'))
414500 郑州市
414530 新乡市

将字典形式编码为 JSON 格式数据:

json.dumps(dic)
'[{"Code": "414500", "Name": "\\u90d1\\u5dde\\u5e02", "level": [{"Code": "414500", "Name": "\\u4e8c\\u4e03\\u533a", "sort": 1}, {"Code": "414500", "Name": "\\u4e2d\\u539f\\u533a", "sort": 2}], "sort": 1}, {"Code": "414530", "Name": "\\u65b0\\u4e61\\u5e02", "level": [{"Code": "414530", "Name": "\\u536b\\u6ee8\\u533a", "sort": 1}, {"Code": "414530", "Name": "\\u7267\\u91ce\\u533a", "sort": 2}], "sort": 2}]'

使用pandas库处理json数据

import pandas as pd
df = pd.read_json('data\city.json',encoding='utf-8')
df
CodeNamelevelsort
0414500郑州市[{'Code': '414500', 'Name': '二七区', 'sort': 1},...1
1414530新乡市[{'Code': '414530', 'Name': '卫滨区', 'sort': 1},...2

处理xml格式数据

数据来源中国气象网北京天气,如下图

<city name="北京">
      <county id="010101" name="北京" weatherCode="101010100"/>
      <county id="010102" name="海淀" weatherCode="101010200"/>
      <county id="010103" name="朝阳" weatherCode="101010300"/>
      <county id="010104" name="顺义" weatherCode="101010400"/>
      <county id="010105" name="怀柔" weatherCode="101010500"/>
      <county id="010106" name="通州" weatherCode="101010600"/>
      <county id="010107" name="昌平" weatherCode="101010700"/>
      <county id="010108" name="延庆" weatherCode="101010800"/>
      <county id="010109" name="丰台" weatherCode="101010900"/>
      <county id="010110" name="石景山" weatherCode="101011000"/>
      <county id="010111" name="大兴" weatherCode="101011100"/>
      <county id="010112" name="房山" weatherCode="101011200"/>
      <county id="010113" name="密云" weatherCode="101011300"/>
      <county id="010114" name="门头沟" weatherCode="101011400"/>
      <county id="010115" name="平谷" weatherCode="101011500"/>
</city>

对xml数据进行处理

import xml.etree.ElementTree as ET
tree = ET.parse('data\weathercode.xml')
root = tree.getroot()
root
<Element 'city' at 0x0000000008459EF8>
for weather in root.iter('county'):
    print(weather.get('id'),weather.get('name'),weather.get('weatherCode'))
010101 北京 101010100
010102 海淀 101010200
010103 朝阳 101010300
010104 顺义 101010400
010105 怀柔 101010500
010106 通州 101010600
010107 昌平 101010700
010108 延庆 101010800
010109 丰台 101010900
010110 石景山 101011000
010111 大兴 101011100
010112 房山 101011200
010113 密云 101011300
010114 门头沟 101011400
010115 平谷 101011500

参考:基于Python数据科学挖掘精华实战课程

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值