目录
0.
列表:列表是对象的有序集合,内容及长度可变 a = [1, 3, 5, 7]
元组:内容不可修改 a = (1, 3, 5, 7)
字典:{键:值} dict = {'abc':123, 98.6:37}
set:无序无重复元素的集合 s = set ([1, 2, 3])
1. 从键盘获取数据
line = input ()
2. 从文本文件获取数据
file = open ()
3. 从excel获取数据
import xlrd
data = xlrd.open_workbook('D:\\test.xlsx')
# 从excel获取数据
# 导入模块
import xlrd
# 打开Excel
data = xlrd.open_workbook('D:\\test.xlsx')
# 1.查看并打印文件中包含sheet的名称
sheet_names = data.sheet_names()
print("sheet names: ", sheet_names)
# 通过索引顺序获取
table = data.sheet_by_index(0)
# 2.获取第一张工作表的行数和列数
nrows = table.nrows
print("rows number: ", nrows)
ncols = table.ncols
print("cols number: ", ncols)
# 3.分别获取第一张工作表的第2行和第2列的值(数组)
print("The value of the 2nd row: ", table.row_values(2))
print("The value of the 2nd row: ", table.col_values(2))
# 4.分别获取特定单元格的值
print("The value of the 1st row of the 1st col: ", table.cell_value(0, 0))
print("The value of the 1st row of the 2nd col: ", table.cell_value(0, 1))
# Code based on Python 3.x
# _*_ coding: utf-8 _*_
# __Author: "LEMON"
import pandas as pd
d = pd.date_range('20170101', periods=7)
aList = list(range(1,8))
df = pd.DataFrame(aList, index=d, columns=[' '])
df.index.name = 'value'
print('----------df.index---------')
print(df.index)
print('---------df.columns---------')
print(df.columns)
print('----------df.values---------')
print(df.values)
print('----------df.describe--------')
print(df.describe)
print('----------information details--------')
print(df.head(2)) #获取开始的n条记录
print(df.tail(3)) #后去最后的n条记录
print(df[3:5]) # df[a:b],获取第a+1至第b-1的记录
# 导入cx_Oracle模块
import cx_Oracle
# 连接oracle数据库
conn = cx_Oracle.connect('D1MDWMGR/adm1210@10.141.71.12/unixdev')
# 创建cursor
cr = conn.cursor()
# sql语句
sql = "select * from test"
cr = cr.execute(sql) # 执行sql语句
# 一次返回所有的结果集使用 fetchall
results = cr.fetchall()
for re in results:
print(re)
# 一次返回一行 fetchone
while (1):
re = cr.fetchone()
if re == None:
break
print(re)
cr.close()
conn.close()
# 从excel获取数据
# 导入模块
import xlrd
# 打开Excel
data = xlrd.open_workbook('D:\\test.xlsx')
# 1.查看并打印文件中包含sheet的名称
sheet_names = data.sheet_names()
print("sheet names: ", sheet_names)
# 通过索引顺序获取
table = data.sheet_by_index(0)
# 2.获取第一张工作表的行数和列数
nrows = table.nrows
print("rows number: ", nrows)
ncols = table.ncols
print("cols number: ", ncols)
# 3.分别获取第一张工作表的第2行和第2列的值(数组)
print("The value of the 2nd row: ", table.row_values(2))
print("The value of the 2nd row: ", table.col_values(2))
# 4.分别获取特定单元格的值
print("The value of the 1st row of the 1st col: ", table.cell_value(0, 0))
print("The value of the 1st row of the 2nd col: ", table.cell_value(0, 1))
# BeautifulSoup.py
import urllib.request
import re
from bs4 import beautiful
def main():
url = "http://baike.baidu.com/view/284853.htm"
response = urllib.request.urlopen(url)
html = response.read()
soup = beautifulsoup(html,"html.parser")
for each in soup.find_all(href = re.compile("view")):
print(each.text, "->", ''.join(["http://baiake.baidu.com",\
each["href"]]))
if __name__ == "__main__":
main()