错题集
1、
a = 'You'
print('\n',a)
print('\n' + a)
原因:第一个是输出两个字符,python中两个字符间自动存在一个空格;第二个是成为了一个字符,所有没有空格
解决方法:
print(’\n’,‘rpg’,sep=’’)
2、
找出列表出重复的元素
https://python3-cookbook.readthedocs.io/zh_CN/latest/c01/p12_determine_most_freqently_items_in_seq.html
3、
删除目录及子文件
import shutil
shutil.rmtree(src)
4、
两个list 求交集,并集,差集,intersection、union、difference
https://blog.csdn.net/bitcarmanlee/article/details/51622263
5、
获取本机ip
ip = socket.gethostbyname(socket.gethostname())
6、
Python中 .to_csv()乱码
解决方法:添加参数 encoding=‘utf_8_sig’
8、eighth issue:
3、一些小的注意点
- 1、用to_excel或者to_csv时,注意设置index=None
- 2、df.x[:2]不包含末端,df.x.loc[:2]包含末端(iloc不包含末端)
4、正则,判断是否匹配到某个字符串,必须全英文
re.compile('^[A-Za-z]+$').match('sdfd') is not None
match未匹配到时,返回None
5、正则,根据某个含关键字的列表,如果字符串包含中间某个关键词,就匹配该字符串(即多对一匹配)
re.compile('|'.join(ket_list)).findall(x)
eg: excel中可能存多对一的mapping,
re.compile('^#$|^&$|^@$|^/$').findall('/')
6、‘银联境外APPLESTOREHONGKONGHKG’,被归到其他而不是香港,HONGKONG 是一个关键词,境外又是一个关键词
- 1、通过优先级顺序,然后match就能解决,
7、df取出一列’value’(几百万行的数据,可以用reduce优化一下)的字符串的str数字,怎么求这列的和
- .apply()转为float类型,然后.sum()
8、根据mapping表,替换几百万列表eg: {‘GUCCI’:‘Y’, ‘百货’:‘N’}, [‘百货xx’, ‘GUCCI’, ‘GUCCIxx’]
- 1、一对多(键相同)使用:df.to_dict(orient=‘list’),然后通过正则re.compile(’|’.join(list)).match() is not None去匹配。
- 2、(暂时还未很好实现)一对一 和 多对一 关系使用:df.to_dict(orient=‘records’),然后applymap(),
5、理解filter、map、reduce、apply、zip用法
- reduce, apply已经替换成:functools.reduce和*、**
参考:https://www.jianshu.com/p/c41627028f58
py2to3:http://www.ttlsa.com/docs/dive-into-python3/porting-code-to-python-3-with-2to3.html
7、寻找单层字典,根据value寻找最大N个值
- 通过zip模块,sorted(zip(xx_dict.values, xx_dict.index), reverse=True)
拓展:https://python3-cookbook.readthedocs.io/zh_CN/latest/c01/p08_calculating_with_dict.html
8、让df类型的mapping表具有顺序matching
-
实现mapping具有先后顺序去matching数据,构建有序列表,有序的遍历取出包含的关键词,用re模块去matching。
拓展:https://python3-cookbook.readthedocs.io/zh_CN/latest/c01/p07_keep_dict_in_order.html -
判断float类型的nan,
np.isnan(math.nan)
math.isnan(np.nan) -
list_duplicates
参考:https://www.the5fire.com/python-remove-duplicates-in-list.html
9、ninth
-
sql server bug 不能用""
-
assert 用法
assert xx (xx为True正常,false报错) -
Pycharm中如何加载多个项目
https://blog.csdn.net/m0_37586991/article/details/73844247
- Python中__repr__和__str__区别
https://blog.csdn.net/luckytanggu/article/details/53649156
print的时候:当两者同时存在时,__str__会覆盖__repr__
偷懒定义__repr__: 定义完str后,直接__repr__ = __str__
https://stackoverflow.com/questions/1436703/difference-between-str-and-repr
10、tenth_weekly
-
用read_sql会将id(product_id, sales)等转为float(后面很多零),但是写回数据库又变正常。
设置coerce_float=False,否则将含有NULL值的整数列转换为浮点数 -
read_sql,read_csv等会读出None类型吗?
不会读出None类型,会转为np.nan
如果设置 na_values=[], keep_default_na=False,None类型将会转为’'的str类型 -
pymssql和adodbapi调用存储过程区别
后者在程序中会返回数据msg,前者返回不了。
Google之后是说都可以返回,只是前者需要构造成sql语句请求,后者类似函数。后续还需要自己测试 -
python调用存储过程的时候,msg[0]不能获取到’success’
要使用adodbapi模块,创建连接。使用pymssql获取不到msg参数 -
re.IGNORECASE相当于 内联标记(?i)
-
newline参数
csv模块,用于区分换行符。解决写入时新增行与行之间的一个空白行问题 -
有无t的参数r、rt区别,所以一般都用带t的模式
rt模式下,python在读取文本时会自动把\r\n转换成\n.
wt模式下,Python写文件时会用\r\n来表示换行。