1. 程式人生 > >python 寫數據到txt 文件

python 寫數據到txt 文件

blog light enc clas cnblogs utf 表示 數組 hat

# 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 文件