python,,pythonw3cs
w3cschool |
菜鸟教程 |
Python123 |
中国大学MOOC |
API reference — pandas 0.25.1 documentation |
库函数
os
os.listdir([path])os.mkdir(path)os.system(string)
os.path
os.path.exists(path)
pathlib
pathlib.Path(path).is_file()pathlib.Path(path).is_dir()pathlib.Path(path).exists()
shutil
shutil.copyfile(src_path, des_path)
re
re.match(pattern, string)re.search(pattern, string)re.sub(pattern, s, string)
pandas as pd
read_excel()read_csv()to_excel()to_csv()Series()DataFrame()apply()
random
random.choice(list)random.shuffle(list)
time
time.ctime()
范例
检测文档的编码格式
import chardetchardet.detect(open('0.csv', 'rb').read())['encoding']
将文件转成utf-8
def getEncoding(file_name): with open(file_name, 'rb') as f: return chardet.detect(f.read())['encoding']def file2utf8(file_name): with open(file_name, 'r', encoding = getEncoding(file_name)) as f: s = f.read() with open(file_name, 'w', encoding = 'utf-8') as f: f.write(s)
Excel数据库互转
import pandas as pdimport sqlite3 as sql# Excel 转 数据库pd.read_excel('age.xls').to_sql(con = sql.connect('age.db'), name = 'age', index = False)# 数据库 转 Excelpd.read_sql('select * from company', con = sql.connect('company.db')).to_excel('company.xls', index = False)
python
评论关闭