以下是小编为大家准备的简单介绍Python中的decode方法的使用,本文共5篇,供大家参考借鉴,希望可以帮助到有需要的朋友。本文原稿由网友“菲宝聪”提供。
篇1:简单介绍Python中的decode方法的使用
这篇文章主要介绍了简单介绍Python中的decode方法的使用,是Python入门学习当中必须掌握的基础知识,需要的朋友可以参考下
decode()方法使用注册编码的编解码器的字符串进行解码,它默认为默认的字符串编码。
语法
以下是decode()方法的语法:
str.decode(encoding=‘UTF-8‘,errors=‘strict‘)
参数
encoding -- 这是所使用的编码。对于所有的编码方案的列表,请访问:标准编码库
errors -- 这可能是给定一个不同的错误处理机制。默认的错误是“严格”,即编码错误提出UnicodeError。其他可能的值是ignore‘, ‘replace‘, ‘xmlcharrefreplace‘, ‘backslashreplace‘ 并通过codecs.register_error().注册的任何其他名称,
返回值
此方法返回的字符串的解码版本。
例子
下面的例子显示了decode()方法的使用。
#!/usr/bin/pythonstr = “this is string example....wow!!!”;str = str.encode(‘base64‘,‘strict‘);print “Encoded String: ” + str;print “Decoded String: ” + str.decode(‘base64‘,‘strict‘)
当我们运行上面的程序,它会产生以下结果:
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=Decoded String: this is string example....wow!!!
篇2:简单介绍Python中的readline方法的使用
这篇文章主要介绍了简单介绍Python中的readline()方法的使用,是Python入门学习中的基础知识,需要的朋友可以参考下
readline()方法从文件中读取一整行,尾部的换行符保持在字符串中。如果大小参数且非负,那么一个最大字节数,包括结尾的换行和不完整的行可能会返回。
遇到EOF时立即返回一个空字符串。
语法
以下是readline()方法的语法:
fileObject.readline( size );
参数
size -- 这是可以从文件中读取的字节数。
返回值
此方法返回从文件中读取的行,
例子
下面的例子显示了readline()方法的使用。
#!/usr/bin/python# Open a filefo = open(“foo.txt”, “rw+”)print “Name of the file: ”, fo.name# Assuming file has following 5 lines# This is 1st line# This is 2nd line# This is 3rd line# This is 4th line# This is 5th lineline = fo.readline()print “Read Line: %s” % (line)line = fo.readline(5)print “Read Line: %s” % (line)# Close opend filefo.close()
当我们运行上面的程序,它会产生以下结果:
Name of the file: foo.txtRead Line: This is 1st lineRead Line: This
篇3:python中使用mysql数据库详细介绍
这篇文章主要介绍了python中使用mysql数据库详细介绍,本文起讲解了安装mysql、安装MySQL-python、mysql 的基本操作、python 操作mysql数据库基础等内容,需要的朋友可以参考下
一、安装mysql
如果是windows 用户,mysql 的安装非常简单,直接下载安装文件,双击安装文件一步一步进行操作即可,
Linux 下的安装可能会更加简单,除了下载安装包进行安装外,一般的linux 仓库中都会有mysql ,我们只需要通过一个命令就可以下载安装:
Ubuntu\\deepin
代码如下:
>>sudo apt-get install mysql-server
>>Sudo apt-get install mysql-client
centOS/redhat
代码如下:
>>yum install mysql
二、安装MySQL-python
要想使python可以操作mysql 就需要MySQL-python驱动,它是python 操作mysql必不可少的模块。
下载地址:pypi.python.org/pypi/MySQL-python/
下载MySQL-python-1.2.5.zip 文件之后直接解压。进入MySQL-python-1.2.5目录:
代码如下:
>>python setup.py install
三、测试
测试非常简单,检查MySQLdb 模块是否可以正常导入。
代码如下:
fnngj@fnngj-H24X:~/pyse$ python
Python 2.7.4 (default, Sep 26 , 03:20:56)
[GCC 4.7.3] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>>import MySQLdb
没有报错提示MySQLdb模块找不到,说明安装OK ,下面开始使用python 操作数据库之前,我们有必要来回顾一下mysql的基本操作:
四、mysql 的基本操作
代码如下:
$ mysql -u root -p (有密码时)
$ mysql -u root (无密码时)
代码如下:
mysql>show databases; // 查看当前所有的数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| csvt |
| csvt04 |
| mysql |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.18 sec)
mysql>use test; //作用与test数据库
Database changed
mysql>show tables; //查看test库下面的表
Empty set (0.00 sec)
//创建user表,name 和password 两个字段
mysql>CREATE TABLE user (name VARCHAR(20),password VARCHAR(20)); Query OK, 0 rows affected (0.27 sec)
//向user表内插入若干条数据
mysql>insert into user values(‘Tom‘,‘1321‘);
Query OK, 1 row affected (0.05 sec)
mysql>insert into user values(‘Alen‘,‘7875‘);
Query OK, 1 row affected (0.08 sec)
mysql>insert into user values(‘Jack‘,‘7455‘);
Query OK, 1 row affected (0.04 sec)
//查看user表的数据
mysql>select * from user;
+------+----------+
| name | password |
+------+----------+
| Tom | 1321 |
| Alen | 7875 |
| Jack | 7455 |
+------+----------+
3 rows in set (0.01 sec)
//删除name 等于Jack的数据
mysql>delete from user where name = ‘Jack‘;
Query OK, 1 rows affected (0.06 sec)
//修改name等于Alen 的password 为 1111
mysql>update user set password=‘1111‘ where name = ‘Alen‘;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
//查看表内容
mysql>select * from user;
+--------+----------+
| name | password |
+--------+----------+
| Tom | 1321 |
| Alen | 1111 |
+--------+----------+
3 rows in set (0.00 sec)
五、python 操作mysql数据库基础
代码如下:
#coding=utf-8
import MySQLdb
conn= MySQLdb.connect(
host=‘localhost‘,
port = 3306,
user=‘root‘,
passwd=‘123456‘,
db =‘test‘,
)
cur = conn.cursor
#创建数据表
#cur.execute(“create table student(id int ,name varchar(20),class varchar(30),age varchar(10))”)
#插入一条数据
#cur.execute(“insert into student values(‘2‘,‘Tom‘,‘3 year 2 class‘,‘9‘)”)
#修改查询条件的数据
#cur.execute(“update student set class=‘3 year 1 class‘ where name = ‘Tom‘”)
#删除查询条件的数据
#cur.execute(“delete from student where age=‘9‘”)
cur.close()
conn.commit()
conn.close()
代码如下:
>>>conn = MySQLdb.connect(host=‘localhost‘,port = 3306,user=‘root‘, passwd=‘123456‘,db =‘test‘,)
Connect() 方法用于创建数据库的连接,里面可以指定参数:用户名,密码,主机等信息,
这只是连接到了数据库,要想操作数据库需要创建游标。
代码如下:
>>>cur = conn.cursor()
通过获取到的数据库连接conn下的cursor()方法来创建游标。
代码如下:
>>>cur.execute(“create table student(id int ,name varchar(20),class varchar(30),age varchar(10))”)
通过游标cur 操作execute()方法可以写入纯sql语句。通过execute()方法中写如sql语句来对数据进行操作。
代码如下:
>>>cur.close()
cur.close() 关闭游标
代码如下:
>>>conn.commit()
conn.commit()方法在提交事物,在向数据库插入一条数据时必须要有这个方法,否则数据不会被真正的插入。
代码如下:
>>>conn.close()
Conn.close()关闭数据库连接
六、插入数据
通过上面execute()方法中写入纯的sql语句来插入数据并不方便。如:
代码如下:
>>>cur.execute(“insert into student values(‘2‘,‘Tom‘,‘3 year 2 class‘,‘9‘)”)
我要想插入新的数据,必须要对这条语句中的值做修改。我们可以做如下修改:
代码如下:
#coding=utf-8
import MySQLdb
conn= MySQLdb.connect(
host=‘localhost‘,
port = 3306,
user=‘root‘,
passwd=‘123456‘,
db =‘test‘,
)
cur = conn.cursor()
#插入一条数据
sqli=“insert into student values(%s,%s,%s,%s)”
cur.execute(sqli,(‘3‘,‘Huhu‘,‘2 year 1 class‘,‘7‘))
cur.close()
conn.commit()
conn.close()
假如要一次向数据表中插入多条值呢?
代码如下:
#coding=utf-8
import MySQLdb
conn= MySQLdb.connect(
host=‘localhost‘,
port = 3306,
user=‘root‘,
passwd=‘123456‘,
db =‘test‘,
)
cur = conn.cursor()
#一次插入多条记录
sqli=“insert into student values(%s,%s,%s,%s)”
cur.executemany(sqli,[
(‘3‘,‘Tom‘,‘1 year 1 class‘,‘6‘),
(‘3‘,‘Jack‘,‘2 year 1 class‘,‘7‘),
(‘3‘,‘Yaheng‘,‘2 year 2 class‘,‘7‘),
])
cur.close()
conn.commit()
conn.close()
executemany()方法可以一次插入多条值,执行单挑sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数。
七、查询数据
也许你已经尝试了在python中通过
代码如下:
>>>cur.execute(“select * from student”)
来查询数据表中的数据,但它并没有把表中的数据打印出来,有些失望。
来看看这条语句获得的是什么
代码如下:
>>>aa=cur.execute(“select * from student”)
>>>print aa
5
它获得的只是我们的表中有多少条数据。那怎样才能获得表中的数据呢?进入python shell
代码如下:
>>>import MySQLdb
>>>conn = MySQLdb.connect(host=‘localhost‘,port = 3306,user=‘root‘, passwd=‘123456‘,db =‘test‘,)
>>>cur = conn.cursor()
>>>cur.execute(“select * from student”)
5L
>>>cur.fetchone()
(1L, ‘Alen‘, ‘1 year 2 class‘, ‘6‘)
>>>cur.fetchone()
(3L, ‘Huhu‘, ‘2 year 1 class‘, ‘7‘)
>>>cur.fetchone()
(3L, ‘Tom‘, ‘1 year 1 class‘, ‘6‘)
...
>>>cur.scroll(0,‘absolute‘)
fetchone()方法可以帮助我们获得表中的数据,可是每次执行cur.fetchone() 获得的数据都不一样,换句话说我没执行一次,游标会从表中的第一条数据移动到下一条数据的位置,所以,我再次执行的时候得到的是第二条数据。
scroll(0,‘absolute‘) 方法可以将游标定位到表中的第一条数据。
还是没解决我们想要的结果,如何获得表中的多条数据并打印出来呢?
代码如下:
#coding=utf-8
import MySQLdb
conn= MySQLdb.connect(
host=‘localhost‘,
port = 3306,
user=‘root‘,
passwd=‘123456‘,
db =‘test‘,
)
cur = conn.cursor()
#获得表中有多少条数据
aa=cur.execute(“select * from student”)
print aa
#打印表中的多少数据
info = cur.fetchmany(aa)
for ii in info:
print ii
cur.close()
conn.commit()
conn.close()
通过之前的print aa 我们知道当前的表中有5条数据,fetchmany()方法可以获得多条数据,但需要指定数据的条数,通过一个for循环就可以把多条数据打印出啦!执行结果如下:
代码如下:
5
(1L, ‘Alen‘, ‘1 year 2 class‘, ‘6‘)
(3L, ‘Huhu‘, ‘2 year 1 class‘, ‘7‘)
(3L, ‘Tom‘, ‘1 year 1 class‘, ‘6‘)
(3L, ‘Jack‘, ‘2 year 1 class‘, ‘7‘)
(3L, ‘Yaheng‘, ‘2 year 2 class‘, ‘7‘)
[Finished in 0.1s]
篇4:python中 ? : 三元表达式的使用介绍
最近更 新
Python 的 with 语句详解
Python中的Numpy入门教程
python正则表达式re模块详解
pyramid配置session的方法教程
python遍历文件夹并删除特定格式文件的示
videocapture库制作python视频高速传输程
使用python解析xml成对应的html示例分享
Python版的文曲星猜数字游戏代码
python访问sqlserver示例
python3图片转换二进制存入mysql
热 点 排 行
Python入门教程 超详细1小时学会
python 中文乱码问题深入分析
比较详细Python正则表达式操作指
Python字符串的encode与decode研
Python open读写文件实现脚本
Python enumerate遍历数组示例应
Python 深入理解yield
Python+Django在windows下的开发
python 文件和路径操作函数小结
python 字符串split的用法分享
篇5:python使用BeautifulSoup分页网页中超链接的方法
作者:令狐不聪 字体:[增加 减小] 类型:转载
这篇文章主要介绍了python使用BeautifulSoup分页网页中超链接的方法,涉及Python使用BeautifulSoup模块操作网页链接的技巧,需要的朋友可以参考下
本文实例讲述了python使用BeautifulSoup分页网页中超链接的方法,分享给大家供大家参考。具体如下:
python通过BeautifulSoup分页网页中的超级链接,这段python代码输出www.jb51.net主页上所有包含了jb51的url链接
from BeautifulSoup import BeautifulSoupimport urllib2import reurl = urllib2.urlopen(“www.jb51.net”)content = url.read()soup = BeautifulSoup(content)for a in soup.findAll(‘a‘,href=True): if re.findall(‘sharejs‘, a[‘href‘]): print “Found the URL:”, a[‘href‘]
希望本文所述对大家的Python程序设计有所帮助,
- Python读写unicode文件的方法2023-01-25
- Python中使用tarfile压缩、解压tar归档文件示例2022-12-19
- Python使用Scrapy爬取妹子图2024-11-01
- 去痘疤痘印简单的方法介绍2023-09-15
- 五种简历使用介绍2025-01-31
- 电脑的使用礼仪介绍2025-04-20
- Python编写屏幕截图程序方法2022-12-11
- 中秋节简单介绍2024-07-27
- 美容方法介绍2023-08-22
- python使用心得之获得github代码库列表2022-12-11