|
|
import codecs
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
|
# 写入文本文件
|
|
|
def write_txt(file_name, content):
|
|
|
with open(file_name, 'w', encoding='utf-8') as f:
|
|
|
f.write(content)
|
|
|
|
|
|
|
|
|
# 读取文本文件
|
|
|
def read_txt(file_name):
|
|
|
with open(file_name, 'r', encoding='utf-8') as f:
|
|
|
return f.readlines()
|
|
|
|
|
|
|
|
|
# 当前时间
|
|
|
def getCurrentTime():
|
|
|
now_time = datetime.now()
|
|
|
str_time = now_time.strftime("%Y-%m-%d %X") # 格式化时间字符串
|
|
|
return str_time
|
|
|
|
|
|
|
|
|
def getCurrentTimeFileName():
|
|
|
now_time = datetime.now()
|
|
|
str_time = now_time.strftime("%Y-%m-%d %X") # 格式化时间字符串
|
|
|
str_time = str_time.replace(" ", "-").replace(":", "-")
|
|
|
return str_time
|
|
|
|
|
|
|
|
|
# 将字符串转为小驼峰
|
|
|
def convert(one_string, space_character): # one_string:输入的字符串;space_character:字符串的间隔符,以其做为分隔标志
|
|
|
string_list = str(one_string).split(space_character) # 将字符串转化为list
|
|
|
first = string_list[0].lower()
|
|
|
others = string_list[1:]
|
|
|
others_capital = [word.capitalize() for word in others] # str.capitalize():将字符串的首字母转化为大写
|
|
|
others_capital[0:0] = [first]
|
|
|
hump_string = ''.join(others_capital) # 将list组合成为字符串,中间无连接符。
|
|
|
return hump_string
|
|
|
|
|
|
|
|
|
# 输出带时间的字符串
|
|
|
def printf(str):
|
|
|
print(getCurrentTime() + " " + str)
|
|
|
|
|
|
|
|
|
# 将文件转换编码形式
|
|
|
def convert_encoding(filename_in, filename_out, encode_in, encode_out):
|
|
|
with codecs.open(filename=filename_in, mode='r', encoding=encode_in) as fi:
|
|
|
data = fi.read()
|
|
|
with open(filename_out, mode='w', encoding=encode_out) as fo:
|
|
|
fo.write(data)
|
|
|
fo.close()
|