读JSON或txt文件常用:
with open(new_path, 'r', encoding='utf-8') as file:
data = json.load(file)
'''
to do
'''
json.loads()
和json.load()
都是将操作对象转换成Python对象,其中json.loads()
是对字符串进行操作,json.load()
是对文件进行操作。
对文件进行修改并保存(至新文件):
with open(old_path, 'r', encoding='utf-8') as file:
content = file.read()
modified_content = '''something to do with content'''
with open(new_path, 'w', encoding='utf-8') as file:
file.write(modified_content)
加载prompt文件常用:
def load_prompt_file(path) -> str:
prompt = ""
with open(path, 'r', encoding='utf-8') as f:
# for line in f.readline():
# prompt += line + '\n'
prompt = f.read()
return prompt
标签:load,文件,常用,prompt,encoding,读写,json,file,path From: https://www.cnblogs.com/05-ivyli-19/p/18339065