Python常见文件操作
1.txt
1.1open()
with open('jilu.txt', 'r') as f: # 打开文件(仅读模式)
get = f.readlines() # 读取每一行,返回值为列表,每个元素包含换行符
get = [x.strip() for x in get] # 将换行符去掉(也可删除元素前后的空格)
with open('jilu.txt', 'w') as f: # 打开文件 写模式
for i in mylist:
f.write(i+'\n') # 带换行写入
f = open('jilu.txt', 'a') # 打开文件 追加形式
f.write('\n')
f.close()
2.json
2.1dump和load (文件有关)
d = [{'name' : 'soap', 'shares' : 90, 'price' : 542.23 }, 1]
with open('jilu.json', 'w') as f: # 将l存储至文件jilu.json中
json.dump(d, f, indent=2) # 会换行并插入tab
# ------------------------------
f = open('jilu1.json', 'r')
get = json.load(f) # 从jilu.json中读取数据 得到为get
f.close()
2.2dumps 和loads
data = {'name' : 'soap', 'shares' : 90, 'price' : 542.23 }
json_str = json.dumps(data)
print(type(json_str)) # <class 'str'>
print(json_str) # {"name": "soap", "shares": 90, "price": 542.23}
# ------------------------------
d = json.loads(json_str)
print(type(d)) # <class 'dict'>
print(d) # {'name': 'soap', 'shares': 90, 'price': 542.23}
3.yaml
3.1 保存
with open(yaml_path, 'w', encoding='utf-8') as f:
save_yaml = {
'names': categories_name,
'nc': len(categories_name),
'train': txt_path,
'test': txt_path
}
yaml.dump(save_yaml, f)
3.2 加载
with open(yaml_path, 'r') as f:
get = yaml.load(f.read())
4. xml
4.1 解析标签
以解析labelimg打标签的xml文件为例
import xml.etree.ElementTree as ET
def get_boxes_from_xml(file_path):
tree = ET.parse(file_path) # 读取并结构化文件
root = tree.getroot() # 找到根
filename = root.find('filename').text # 从根找filename标签的值并转为字符
# object = root.find('object')
info = []
for object in root.findall('object'): # 从根找出所有object标签的对象
name = object.find('name').text # 在object对象层找name标签对应的字符
bandbox = object.find('bndbox')
xmin = int(bandbox.find('xmin').text)
ymin = int(bandbox.find('ymin').text)
xmax = int(bandbox.find('xmax').text)
ymax = int(bandbox.find('ymax').text)
each = [name, xmin, ymin, xmax, ymax]
info.append(each)
return info
5. logging
5.1 简易使用logging
# fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s: %(message)s' # 详细输出
fmt = '%(message)s' # 仅输出info
logging.basicConfig(level=logging.INFO, filename='log.txt', format=fmt)
logger = logging.getLogger(__name__)
for i in ['a', 'b', 'c']:
logger.info(i)
print(i)
time.sleep(1)
5.2 复杂版
def init_logger(log_file=None, log_dir=None, log_level=logging.INFO, mode='w', stdout=True):
"""
log_dir: 日志文件的文件夹路径
mode: 'a', append; 'w', 覆盖原文件写入.
"""
import datetime
def get_date_str():
now = datetime.datetime.now()
return now.strftime('%Y-%m-%d_%H-%M-%S')
fmt = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s: %(message)s'
if log_dir is None:
log_dir = '~/temp/log/'
if log_file is None:
log_file = 'log_' + get_date_str() + '.txt'
if not os.path.exists(log_dir):
os.makedirs(log_dir)
log_file = os.path.join(log_dir, log_file)
# 此处不能使用logging输出
print('log file path:' + log_file)
logging.basicConfig(level=logging.DEBUG,
format=fmt,
filename=log_file,
filemode=mode)
if stdout:
console = logging.StreamHandler(stream=sys.stdout)
console.setLevel(log_level)
formatter = logging.Formatter(fmt)
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
return logging
logging = init_logger(log_dir='log') # 初始化
logging.info('my message') # 会打印出来
六、AsciiTable
from terminaltables import AsciiTable
headers = [['stream', 'records', 'schemas']]
rows = [[s.name, s.num_records, s.num_schemas]
for s in summary.streams.values()]
data = headers + rows
table = AsciiTable(data)
print(table.table)
table = AsciiTable(data)
print(table.table)