Python-文件操作

  1. 读文件
  2. Json

读文件

try:
   # 'w'表示写,'r'表示读,'a'(append)标识追加模式
   my_file=open('file.txt','w')
except Exception as e:
   print(e)

# 可以同时打开多个文件
with open('./data/code.txt', 'r', encoding='utf-8') as read_file,open('./data/write.txt', 'w', encoding='utf-8') as write_file:
    contents=read_file.readlines()
    print(contents)

一行一行地读用readline(),读取停用词表:

key_words = [word.strip() for word in open("stop_words.txt", "r", encoding="utf-8").readlines()]

Json

从文件中加载json:

with open("../config/record.json",'r') as load_f:
   load_dict = json.load(load_f)

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 changzeyan@foxmail.com

×

喜欢就点赞,疼爱就打赏