如何在 python 中简化保存文件的过程
How to simplify the process of saving files in python
如何更轻松地读写python中的文件?一条线?函数的变体可以用于:
with open(file='file_name', mode='r', encoding='utf8') as f:
F = f.read()
使阅读更容易json
# pip install varname
def read_j(file_name, encod='utf8'):
with open(file_name, 'r', encoding=encod) as f:
F = json.load(f)
return F
让写作更简单json
def write_j(data, file_name=None, type_='w', indent=4, ensure_ascii=False, encod='utf8'):
if file_name == None:
file_name = varname.nameof(data, frame=2) + '.json'
with open(file_name, type_, encoding=encod) as f:
json.dump(data, f, indent=indent, ensure_ascii=ensure_ascii)
使阅读文件更容易
def read_f(file_name, encod='utf8'):
with open(file_name, 'r', encoding=encod) as f:
F = f.read()
return F
写文件更简单
def write_f(data, file_name=None, type_='w', encod='utf8'):
# print(data)
if file_name == None:
file_name = varname.nameof(data, frame=2)
if type(data) == list or type(data) == dict:
write_j(data, file_name+'.json')
elif type(data) == str:
file_name = file_name + '.txt'
with open(file_name, type_, encoding=encod) as f:
f.write(data)
elif type(data) == bytes:
with open(file_name, mode='wb', encoding=None) as f:
f.write(data)
else:
with open(file_name, type_, encoding=encod) as f:
f.write(data)
如何更轻松地读写python中的文件?一条线?函数的变体可以用于:
with open(file='file_name', mode='r', encoding='utf8') as f:
F = f.read()
使阅读更容易json
# pip install varname
def read_j(file_name, encod='utf8'):
with open(file_name, 'r', encoding=encod) as f:
F = json.load(f)
return F
让写作更简单json
def write_j(data, file_name=None, type_='w', indent=4, ensure_ascii=False, encod='utf8'):
if file_name == None:
file_name = varname.nameof(data, frame=2) + '.json'
with open(file_name, type_, encoding=encod) as f:
json.dump(data, f, indent=indent, ensure_ascii=ensure_ascii)
使阅读文件更容易
def read_f(file_name, encod='utf8'):
with open(file_name, 'r', encoding=encod) as f:
F = f.read()
return F
写文件更简单
def write_f(data, file_name=None, type_='w', encod='utf8'):
# print(data)
if file_name == None:
file_name = varname.nameof(data, frame=2)
if type(data) == list or type(data) == dict:
write_j(data, file_name+'.json')
elif type(data) == str:
file_name = file_name + '.txt'
with open(file_name, type_, encoding=encod) as f:
f.write(data)
elif type(data) == bytes:
with open(file_name, mode='wb', encoding=None) as f:
f.write(data)
else:
with open(file_name, type_, encoding=encod) as f:
f.write(data)