python将字符串以utf-8格式保存在txt文件中的方法
作者:哈哈儿_ 时间:2023-01-10 14:04:55
如下所示:
#ltp_data 字符串 写进777.txt
1、
def save(filename, contents):
fh = open(filename, 'w', encoding='utf-8')
fh.write(contents)
fh.close()
save('F:\\ltp-3.3.1-win-x86\\777.txt', ltp_data)
2、
#这句话自带文件关闭功能,所以和那些先open再write再close的方式来说,更加pythontic!
with open("F:\\ltp-3.3.1-win-x86\\777.txt", "w", encoding='utf-8') as f:
f.write(ltp_data)
3、
fh = open('F:\\ltp-3.3.1-win-x86\\777.txt', 'w', encoding='utf-8')
fh.write(ltp_data)
fh.close()
4、常见mode
来源:https://blog.csdn.net/qq_22521211/article/details/80052085
标签:python,utf-8,txt
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL数据库搜索中的大小写敏感性
2008-11-24 12:44:00
对json字符串与python字符串的不同之处详解
2023-11-22 04:35:39
WEB前端开发经验总结
2009-07-20 18:01:00
![](https://img.aspxhome.com/file/UploadPic/20097/20/standard-1-62s.jpg)
解决jupyter加载文件失败的问题
2022-07-21 19:17:05
![](https://img.aspxhome.com/file/2023/2/85062_0s.jpg)
python打印当前文件的绝对路径并解决打印为空的问题
2023-10-26 16:24:00
Go语言实现钉钉发送通知
2024-05-08 10:44:29
JavaScript获取中英文混合字符串长度的方法示例
2024-05-02 16:27:25
使用pandas实现连续数据的离散化处理方式(分箱操作)
2023-08-28 21:45:51
Python Selenium 之数据驱动测试的实现
2021-12-16 22:45:27
![](https://img.aspxhome.com/file/2023/9/68259_0s.png)
CentOS7服务器中apache、php7以及mysql5.7的安装配置代码
2023-11-19 02:14:52
在Lighttpd服务器中运行Django应用的方法
2021-08-05 20:19:50
W3C web标准概念入门
2008-05-25 15:06:00
CSS3中的box-sizing属性
2010-04-05 21:52:00
![](https://img.aspxhome.com/file/UploadPic/20104/5/20090424070002186-44s.png)
python判断字符串的前两个字母是否是"id"的示例代码
2021-05-02 20:39:12
2014 年最热门的21款JavaScript框架推荐
2024-04-16 08:48:08
![](https://img.aspxhome.com/file/2023/5/137015_0s.jpg)
MySQL全局锁和表锁的深入理解
2024-01-24 00:48:53
![](https://img.aspxhome.com/file/2023/2/113982_0s.png)
Matplotlib使用Cursor实现UI定位的示例代码
2022-04-18 15:27:13
![](https://img.aspxhome.com/file/2023/4/90534_0s.png)
asp Response.flush 实时显示进度
2011-04-14 10:31:00
SQL实现LeetCode(185.系里前三高薪水)
2024-01-18 19:12:22
css2.1实现多重背景和边框效果
2010-06-23 19:02:00
![](https://img.aspxhome.com/file/UploadPic/20106/23/01-35s.jpg)