Python数据处理(1)

本文介绍了Python中用于数据类型的内置工具type, dir, help,并探讨了易于机器处理的数据格式CSV, JSON, XML。展示了如何导入CSV、JSON和XML文件中的数据。" 122188838,9830261,Jira Software 过去一年关键功能升级解析,"['Jira', 'Atlassian', '项目管理', '工作流', '软件工具']

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

1.python中,有几个内置的工具:type, dir, help:

    type:可以帮助确定我们的对象属于哪种数据类型,在python中,将变量放到type()的括号中,

    dir:会返回一个内置方法与属性的列表,帮我们列出特定数据类型能做的所有事情,举例用字符串‘cat,dog,horse’

>>> dir('cat,dog,horse')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__'
, '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '
__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__ne
w__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__'
, '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith',
'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal',
 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper',
'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust',
 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title'
, 'translate', 'upper', 'zfill']

      help:返回对象,方法或模块的文档。

2.数据可以存储成许多不同的格式和文件类型,某些格式存储的数据很容易被机器处理,还有一些容易被人工读取,微软word文档属于后者,而CSV,JSON,XML文件属于前者。

• 逗号分隔值(Comma-Separated Values,CSV)
• JavaScript 对象符号(JavaScript Object Notation,JSON)
• 可扩展标记语言(eXtensible Markup Language,XML)
在口语和书面语中,提到这些数据格式时通常使用它们的短名字(如 CSV)。

如何导入csv

data-text.csv里面的数据如下:

"Year","Country","Sex","Display Value","Numeric"
"1990","Andorra","Both sexes","77","77.00000"
"2000","Andorra","Both sexes","80","80.00000"
"2012","Andorra","Female","28","28.00000"
"2000","Andorra","Both sexes","23","23.00000"
"2012","United Arab Emirates","Female","78","78.00000"
"2000","Antigua and Barbuda","Male","72","72.00000"
"1990","Antigua and Barbuda","Male","17","17.00000"
"2012","Antigua and Barbuda","Both sexes","22","22.00000"
"2012","Australia","Male","81","81.00000"
import csv

csvfile = open('data-text.csv', 'rt')   #如果csv里面是二进制的,需要用'rb',如果是文本就用‘rt’
reader = csv.reader(csvfile)
for row in reader:
    print(row)

如何导入json

json文件中的数据:

[
{
"Indicator":"Life expectancy at birth (years)",
"PUBLISH STATES":"Published",
"Year":1990,
"WHO region":"Europe",
"World Bank income group":"High-income",
"Country":"Andorra",
"Sex":"Both sexes",
"Display Value":77,
"Numeric":77.00000,
"Low":"",
"High":"",
"Comments":""
}
]
import json

json_data = open('data-text.json').read()
data = json.loads(json_data)

for item in data:
    print(item)

如何导入XML数据

xml文件中的数据:

省略

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值