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文件中的数据:
省略