读写UTF-8文件(Python 2)
import codecs
#读
f = codecs.open("you_file.txt", "r", "utf-8")
line = f.readline()
#写
f = codecs.open("you_file.txt", "w", "utf-8")
txt = unicode("中文1", "utf-8")
f.write(txt)
f.write(u'中文2\n')
f.flush()
f.close()
另外Python文件系统操作实例详解
获取路径问题
获取执行该文件所在的目录(参考),也就相当于pwd:
os.getcwd()和os.path.abspath(os.curdir)
获取当前文件所在的目录:
os.path.dirname(os.path.realpath(__file__))
gld@MacBook ~ % /Users/gld/opt/miniconda3/bin/python /Users/gld/test/verify.py
Out:
/Users/gld
/Users/gld/test
时间问题
string -> datetime:
datetime.datetime.strptime("2017-11-23 16:10:10", "%Y-%m-%d %H:%M:%S")
datetime -> string:
dt.strftime("%Y-%m-%d %H:%M:%S")
dt = datetime.datetime(2019, 8, 1)
dt.isocalendar() # (2019, 31, 4),(2019年,第31周,星期四)
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(pathname)s(%(lineno)d) - (%(process)d->%(thread)d): %(message)s')
logging.info('Start app...')
logging.info('Success!')
输出:
2019-12-03 15:17:14,817 - INFO - D:/Code/PycharmProjects/gld/tmp/tmp.py(26) - (26024->29276): Start app...
2019-12-03 15:17:14,817 - INFO - D:/Code/PycharmProjects/gld/tmp/tmp.py(27) - (26024->29276): Success!
%(levelno)s: 打印日志级别的数值
%(levelname)s: 打印日志级别名称
%(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
%(filename)s: 打印当前执行程序名
%(funcName)s: 打印日志的当前函数
%(lineno)d: 打印日志的当前行号
%(asctime)s: 打印日志的时间
%(thread)d: 打印线程ID
%(threadName)s: 打印线程名称
%(process)d: 打印进程ID
%(message)s: 打印日志信息
打印级别有5个级别,DEBUG,.INFO,WARNING,ERROR,CRITICAL,默认是WARNING
打印到屏幕同时输出到日志文件:
import logging
import sys
fmt = logging.Formatter(
'%(asctime)s - %(levelname)s - %(pathname)s(%(lineno)d) - (%(process)d->%(thread)d): %(message)s')
logger = logging.getLogger('app')
logger.setLevel(logging.INFO)
# 输出到屏幕
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(fmt)
# 输出到文件
file_handler = logging.FileHandler('./app.log')
file_handler.setFormatter(fmt)
# 最后别忘了添加
logger.addHandler(console_handler)
logger.addHandler(file_handler)
# 记得是用前面定义的logger
logger.warning('Start app...')
logger.info('Success!')
import pandas as pd
df1 = pd.DataFrame({'a': [3, 1], 'b': [4, 3]})
df2 = df1.copy()
df3 = df1.copy()
dfs = [df1, df2, df3]
sheet_names = ['a', 'b', 'c']
with pd.ExcelWriter('D:\\output.xlsx') as writer:
for idx in range(len(dfs)):
dfs[idx].to_excel(writer, sheet_name=sheet_names[idx])
writer.save()
writer.close()
月份加减
import calendar
import datetime
def add_months(start, months):
year = start.year + months // 12
month = (start.month + months % 12) % 12
if month == 0:
month = 12
day = start.day
max_day = calendar.monthrange(year, month)[1] # 获取某个月最多多少天
if day > max_day:
day = max_day
return datetime.datetime(year, month, day, hour=start.hour, minute=start.minute, second=start.second,
microsecond=start.microsecond)
result = add_months(datetime.datetime.strptime(str(20210131), '%Y%m%d'), -1)
import re
标志位:
re.I = re.IGNORECASE,忽略大小写,默认考虑大小写
re.S = re.DOTALL,点号(.)匹配换行符,默认不匹配换行符
re.M = re.MULTILINE,多行匹配模式,默认单行匹配模式