python 写数据到txt 文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 写数据到txt 文件相关的知识,希望对你有一定的参考价值。
# coding=utf-8 import codecs # list = [[1,2],[3,4]] list = [‘{"PN":"34VT123","DUID":"XXXX","location":{"coordinates":[-100.35256443,33.4586858]},"SPD":125,"DT":"2017-06-02T13:15:20Z","driverID":"XXXXX","cate":"event","subCate":"OBDII","eventOBDII":{"what":"emergencyBrake","param":2,"GID":123456479}}‘,‘{"PN":"34VT123","DUID":"XXXX","location":{"coordinates":[-100.35256443,33.4586858]},"SPD":125,"DT":"2017-06-03T13:15:20Z","driverID":"XXXXX","cate":"event","subCate":"OBDII","eventOBDII":{"what":"emergencyBrake","param":2,"GID":123456479}}‘] f = codecs.open("main.txt",‘w‘,‘utf-8‘) #f.write(str(list)) for i in list: f.write(str(i)+‘\n‘) #\n为换行符 f.close()
python数组写入txt
#coding=utf-8 import codecs list = [[1,2],[3,4]] s = u‘亚像素精度:\r\n‘ #u表示读取中文,\r\n为换行符 f = codecs.open("main.txt",‘w‘,‘utf-8‘) f.write(s) #f.write(str(list)) for i in list: f.write(str(i)+‘\r\n‘) #\r\n为换行符 f.close()
以上是关于python 写数据到txt 文件的主要内容,如果未能解决你的问题,请参考以下文章
编写一个程序, 将 a.txt 文件中的单词与 b.txt 文件中的 单词交替合并到 c.txt 文件中, a.txt 文件中的单词用回车符 分隔, b.txt 文件中用回车或空格进行分隔。(代码片段