python——数据获取

目录

 

1. 从键盘获取数据

2. 从文本文件获取数据


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()

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值