自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(17)
  • 收藏
  • 关注

原创 编码和解码十六进制数字

import binascii, base64s = b'Hello, world!'# 字节流编码为十六进制数h = binascii.b2a_hex(s)print(h)  # b'48656c6c6f2c20776f726c6421'# 十六进制数解码为字节流s1 = binascii.a2b_hex(h)print(s1)  # b'Hello, world!'# 用...

2019-01-14 07:28:26 1876

原创 同关系型数据库进行交互

import sqlite3stocks = [    ('GOOG', 100, 490.1),     ('AAPL', 50, 545.75),    ('FB', 150, 7.45),    ('HPQ', 75, 33.2), ]db = sqlite3.connect('database.db')sqlite3.connect(database [,timeou...

2019-01-14 07:24:46 191

原创 xml.etree.ElementTree

from urllib.request import urlopenfrom xml.etree.ElementTree import parseu = urlopen('http://planet.python.org/rss20.xml')doc = parse(u)for item in doc.iterfind('channel/item'):    title = item...

2019-01-14 07:05:47 245

原创 urllib

urllib.request.urlopen()函数用于实现对目标url的访问。函数原型如下:urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)函数参数介绍1. url 参数:目标资源在网路中的位置。可以是一个表示URL的字符...

2019-01-14 06:55:40 341

原创 读写JSON数据

import jsondata = {    'name': 'ACME',     'shares': 100,     'price': 542.23}1、将Python数据结构转换为JSONjson_str = json.dumps(data)print(json_str)  # {"name": "ACME", "shares": 100, "price": 542....

2019-01-14 06:48:51 397

原创 读写csv数据

import csvfrom collections import namedtuple1、将csv数据读取为列表with open('stocks.csv') as f:    f_csv = csv.reader(f)    headers = next(f_csv)    print(headers)  # ['Symbol', 'Price', 'Date', 'Time'...

2019-01-07 07:11:56 370

原创 同串口进行通信

通过串口读取和写入数据,一般是同某种硬件设备(机器人或传感器)进行交互,使用serial包打开一个串口,然后通过read(),readline()和write()来读写数据import serialser = serial.Serial('/dev/tty.usbmodem641', # Device name varies                    baudrate=960...

2019-01-07 07:01:18 117

原创 序列化Python对象

import pickledata = ... # Some Python objectf = open('somefile', 'wb')pickle.dump(data, f)# 要将对象转储为字符串,可以使用pickle.dumps()s = pickle.dumps(data)# 从字节流中重新创建出对象,使用pickle.load()或者pickle.loads()#...

2019-01-07 06:58:08 452

原创 创建临时文件和目录

from tempfile import TemporaryFile, NamedTemporaryFile, TemporaryDirectoryimport tempfilewith TemporaryFile('w+t') as f:    # Read/write to the file    f.write('Hello World\n')    f.write('Testi...

2019-01-07 06:36:16 522

原创 os模块

import osimport time1、os.pathpath = '/Users/beazley/Data/data.csv'# 返回文件名print(os.path.basename(path))  # data.csv# 返回文件路径print(os.path.dirname(path))  # /Users/beazley/Data# 获得绝对路径os.pa...

2019-01-07 06:26:38 100

原创 模拟出普通文件进行字符串I/O操作

import ios = io.StringIO()s.write('Hello World\n')print('This is a test', file=s)print(s.getvalue())# Hello World# This is a tests1 = io.StringIO('Come!!!')print(s1.read(4))# ComeStringIO...

2019-01-07 06:09:50 152

原创 itertools.chain

from itertools import chaina = [1, 2, 3, 4]b = ['x', 'y', 'z']for x in chain(a, b):    print(x, end='')  # 1234xyz我们需要对许多对象执行相同的操作,但是这些对象包含在不同的容器内,chain可以在不同容器中进行迭代,并且避免写出嵌套的循环处理chain()可接受一个或多...

2019-01-07 06:05:24 532

原创 读写二进制数据

二进制数据在python中以字节(bytes)类型和字节数组类型(bytearray)保存着,前者数据固定,后者不固定,可继续添加。其每个元素为一个字节的数值,一字节由8位二进制数字组成,换算为10进制,位于0-255之间。ASCII码:一个英文字母占一个字节的空间,一个中文汉字占两个字节的空间。UTF-8编码:一个英文字符等于一个字节,一个中文(含繁体)占三个字节Unicode编码:一个英...

2018-12-28 17:54:55 2586

转载 读写文本数据

1、读操作在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开一个文件对象(通常称为文件描述符),然后,通过操作系统提供的接口从这个文件对象中读取数据(读文件),或者把数据写入这个文件对象(写文件)。with open('somefile.txt', 'rt') as f:    data = f.read()  调...

2018-12-28 17:51:56 255

原创 print()函数

将print()函数的输出重定向到一个文件中,加上file关键字参数即可。需要保证文件是以文本模式打开的,如果文件是二进制模式打开的,就会失败with open('somefile.txt', 'at') as f:    print('Hello World World!', file=f) 在print()函数中使用sep和end关键字参数来修改输出。test_lsit = ...

2018-12-25 18:13:52 960

原创 heapq.merge

一组有序序列,想对它们合并在一起之后的有序序列进行迭代import heapqa = [1, 1, 7, 10]b = [2, 5, 6, 11]for c in heapq.merge(a, b):    print(c, end='')  # 1125671011with open('sorted_file_1.txt', 'rt') as file1, \     op...

2018-12-25 18:13:29 380

原创 yield from

# 扁平化处理嵌套型的序列from collections import Iterabledef flatten(items, ignore_types=(str, bytes)):    for x in items:        if isinstance(x, Iterable) and not isinstance(x, ignore_types):            ...

2018-12-20 08:41:01 76

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除