软件版本:Python3
操作系统:Raspbian


Python库安装:

$ sudo pip3 install xxxx

临时指定软件源: 仅本次命令有效

$ sudo pip3 install xxxx  -i https://pypi.tuna.tsinghua.edu.cn/simple  

设置默认软件源:

$ sudo pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

实际操作是在用户家目录下新建文件 ~/.pip/pip.conf

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

有的系统默认是~/.config/pip/pip.conf,但建立在~/.pip/pip.conf也行。
/etc/pip.conf文件中也可以设置源,与用户目录中设置的可同时使用,用户设置优先。


常用的国内镜像:

清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/


Python脚本描述:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

标准输出:(默认换行)

print('Hello World')

输出不换行:

print('Hello World', end='')

格式化输出:

print('Hello %s, you have $%d.' % ('Alexander', 1000000))

格式化与C语言一致


标准输入:

name = input('please enter your name: ')
print("Hello", name)

将字符转换为Unicode编码:

ord("中")
ord("A")

将Unicode编码转换成字符:

chr(65)
chr(20013)

字符编码:转换成byte类型

>>> 'ABC'.encode('ascii')
b'ABC'
>>> '中文'.encode('utf-8')
b'\xe4\xb8\xad\xe6\x96\x87'

字符解码:

 b'\xe4\xb8\xad\xe6\x96\x87'.decode('utf-8')
'中文'

 b'\xe4\xb8\xad\xff'.decode('utf-8', errors='ignore')

errors='ignore' 表示忽略错误


计算字符串长度:

>>> len('ABC')
3
>>> len('中文')
2
>>> len('中文'.encode('utf-8'))
6

时间戳:

import time
print time.time()