初学Python问题集锦
2017-08-26 15:32
357 查看
1、SyntaxError: Missing parentheses in call to 'print'
遇到此类问题主要是版本问题,python 2可以提供print x,而python3只能print(x)
2、模块导入的两种方式
1)eg:
import math as myMath
print(myMath.sin(30))
2)eg:from 模块import 对象名【as别名】 #只能通过别名访问
from math import sin as mysin
print(mysin(30))
3、强制类型转换
TypeError: can't multiply sequence by non-int of type 'str'
eg:int(input())
4、正则表达式match匹配问题
match只从第一个字符开始匹配,只有第一个字符匹配上才匹配后面的,否则会提示下面错误:
Traceback (most recent call last):
File "C:/Users/Administrator/AppData/Local/Programs/Python/Python35/he.py", line 5, in <module>
print(m.group(0))
AttributeError: 'NoneType' object has no attribute 'group'
若要选择match匹配,则需要加个判断或者异常处理即可,或者使用re.findall()或re.search()
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
import re
text="JGood is a handsome body,he is cool,clever,and so on"
m=re.match(r"(\w+)\s",text)
if m:
print(m.group(0),'\n',m.group(1))
else:
print("not match")
结果:================ RESTART: C:/Users/Administrator/Desktop/h.py ================
JGood
JGood
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
5、爬虫初尝(糗事百科)
-----------------------------------------------------------------------------------
内容太多就上个代码图
遇到此类问题主要是版本问题,python 2可以提供print x,而python3只能print(x)
2、模块导入的两种方式
1)eg:
import math as myMath
print(myMath.sin(30))
2)eg:from 模块import 对象名【as别名】 #只能通过别名访问
from math import sin as mysin
print(mysin(30))
3、强制类型转换
TypeError: can't multiply sequence by non-int of type 'str'
eg:int(input())
4、正则表达式match匹配问题
match只从第一个字符开始匹配,只有第一个字符匹配上才匹配后面的,否则会提示下面错误:
Traceback (most recent call last):
File "C:/Users/Administrator/AppData/Local/Programs/Python/Python35/he.py", line 5, in <module>
print(m.group(0))
AttributeError: 'NoneType' object has no attribute 'group'
若要选择match匹配,则需要加个判断或者异常处理即可,或者使用re.findall()或re.search()
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
import re
text="JGood is a handsome body,he is cool,clever,and so on"
m=re.match(r"(\w+)\s",text)
if m:
print(m.group(0),'\n',m.group(1))
else:
print("not match")
结果:================ RESTART: C:/Users/Administrator/Desktop/h.py ================
JGood
JGood
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
5、爬虫初尝(糗事百科)
-----------------------------------------------------------------------------------
内容太多就上个代码图
相关文章推荐
- python初学常见问题记录(4)
- html初学问题集锦
- Django Python MySql Jquery Json 问题集锦
- 初学Python的问题记录1
- python 初学一问题
- 初学maven(1)-常见小问题集锦
- python初学遇到的问题总结
- [Mark] Spark+Python 初学时遇到的问题
- python初学的各种错误集锦
- python 相关问题集锦
- python初学常见问题记录(1)--linux下和win7下安装python
- python初学问题1
- python初学遇到的问题汇总
- python初学常见问题记录(2)--eclipse下报错及乱码
- Python初学注意问题
- python初学常见问题记录(4.1)-解决了输出覆盖和提示不对应问题
- 初学python遇到的一个小问题
- Python初学问题集
- 初学Python遇到的问题
- 初学Python遇到的问题一二