python文本处理
python文本操作备忘
Arms206
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
python os 目录路径问题
用os.getcwd()获取到的是服务器根目录的路径,而不是文件所在的项目文件里的路径从别人源代码里注意了到此问题的解决方法,ROOT_DIR = os.path.dirname(os.path.realpath(__file__))参考代码 github :ResEncoder/config.py at master · easonnie/ResEncoder · GitHubThis repo is for residual-connected sentence encoder fo.原创 2021-11-07 18:24:48 · 718 阅读 · 0 评论 -
python文本处理正则表达式备忘
参考:--------廖雪峰python教程---------import re re.substring = "Men's voices are loud and clear." 将string中的's替换为空格'sstring = re.sub(r"'s", r" 's", string)原创 2021-04-20 15:14:32 · 143 阅读 · 0 评论 -
python文本处理将每轮结果写入文本文件
保存每轮的准确率Acc,F1值到文本文件中s = 'epoch:{}, train_acc:{}, train_f1:{}, dev_acc:{}, dev_f1:{}'.format(epoch,train_acc,train_f1,dev_acc,dev_f1)with open(os.path.join('test_folds','save_result.txt'), 'a', encoding = 'utf-8') as f: f.write(s) f.wr...原创 2021-04-20 11:24:06 · 1286 阅读 · 0 评论 -
python数据处理
也是参考的别人的代码,忘了是github中哪个了,感谢 统计一个概率,比如 my_std 这个值能大于 my_list 中 95% 的值import numpy as np# my_list: [1270, 235, 2103]my_list = np.array( my_list )# 平均值np.mean( my_list )# 均值加上标准差的2倍my_std = np.mean( my_list ) + 2 * np.std( my_list )#...原创 2021-04-20 11:20:51 · 145 阅读 · 0 评论 -
python文本处理读取文本文件
test.txt: 孔老二 罪恶的 一生# lines -> ['孔老二\n', '罪恶的\n', '一生']with open('test.txt', encoding = 'utf-8') as f: lines = f.readlines()原创 2021-04-20 11:07:25 · 215 阅读 · 0 评论 -
python文本处理中文分词
对文档(多个句子)进行中文分词: a_doc :['孔丘开办私塾,学费要十条腊肉','听孔老二讲学的,都是贵族、官宦人家的子弟。','清代乾嘉学派考证儒学多为造假']import jiebadef cut_words(a_list, a_function): return [a_function(x) for x in a_list]a_list_seg = cut_words(a_doc, lambda x: " ".join( jieba.cut(x) ..原创 2021-04-20 10:51:14 · 1060 阅读 · 0 评论 -
python文本处理json备忘
json中文编码出错比如\u9644\u67d1\u4e52 ensure_ascii = False 加上这个就好了位置:with open('demo.json', 'a', encoding = 'utf-8') as f: json.dump(data, f, ensure_ascii = False)以一种易读的格式写入json文件读取json文件有多行json对象原创 2021-04-20 10:36:22 · 323 阅读 · 0 评论
分享