国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python 編寫輸出到csv的操作

瀏覽:84日期:2022-06-24 13:46:19
如下所示:

def test_write(self): fields=[] fields.append(orderCode) with open(r’./test001.csv’, ’a’,newline='') as f: writer = csv.writer(f) writer.writerow(fields)

python 編寫輸出到csv的操作

定義一個列表:然后將需要寫的數(shù)據(jù)添加到列表

a #追加數(shù)據(jù)到下一行

newline=’’ //去除每一行中間的空行,若不加的話,行與行中間有空格

加newline

python 編寫輸出到csv的操作

不加:

python 編寫輸出到csv的操作

python 編寫輸出到csv的操作

補充:利用python將json數(shù)據(jù)轉(zhuǎn)換為csv格式

假設(shè).json文件中存儲的數(shù)據(jù)為:

{'type': 'Point', 'link': 'http://www.dianping.com/newhotel/22416995', 'coordinates': [116.37256372996957, 40.39798447055443], 'category': '經(jīng)濟型', 'name': '北京荷塘山莊', 'count': '278', 'address': '北京市懷柔區(qū)黃花城村安四路', 'price': '380'}{'type': 'Point', 'link': 'http://www.dianping.com/newhotel/19717653', 'coordinates': [116.56881588256466, 40.43310967948417], 'category': '經(jīng)濟型', 'name': '慕田峪長城魚師傅鄉(xiāng)村酒店', 'count': '89', 'address': '北京市懷柔區(qū)渤海鎮(zhèn)葦?shù)甏?慕田峪長城下3公里處,近懷黃路)', 'price': '258'}{'type': 'Point', 'link': 'http://www.dianping.com/newhotel/58365289', 'coordinates': [116.62874974822378, 40.45610264855833], 'category': '經(jīng)濟型', 'name': '北京蜜桃兒親子客棧', 'count': '119', 'address': '北京市懷柔區(qū)神堂峪風景區(qū)下官地11號', 'price': '549'}

現(xiàn)在需要將上面的這些數(shù)據(jù)存為csv格式,其中字典的keys為csv中的屬性名稱,字典的values為csv中屬性對應(yīng)的值。

如果只需要按照json的keys來生成csv,那么操作比較簡單,直接按照下面的方法即可:

#-*-coding:utf-8-*-import csvimport jsonimport sysimport codecsdef trans(path): jsonData = codecs.open(path+’.json’, ’r’, ’utf-8’) # csvfile = open(path+’.csv’, ’w’) # 此處這樣寫會導(dǎo)致寫出來的文件會有空行 # csvfile = open(path+’.csv’, ’wb’) # python2下 csvfile = open(path+’.csv’, ’w’, newline=’’) # python3下 writer = csv.writer(csvfile, delimiter=’t’, quoting=csv.QUOTE_ALL) flag = True for line in jsonData: dic = json.loads(line[0:-1]) if flag: # 獲取屬性列表 keys = list(dic.keys()) print (keys) writer.writerow(keys) # 將屬性列表寫入csv中 flag = False # 讀取json數(shù)據(jù)的每一行,將values數(shù)據(jù)一次一行的寫入csv中 writer.writerow(list(dic.values())) jsonData.close() csvfile.close()if __name__ == ’__main__’: path=str(sys.argv[1]) # 獲取path參數(shù) print (path) trans(path)

在python3下運行,命令行輸入

python C:UsersMaMQDocumentsjsonToCsv.py C:UsersMaMQDocumentsdatageoFood

其中第三個參數(shù)為需要轉(zhuǎn)換的文件的路徑和其名稱,將其后綴刪除。運行文件后即可得到轉(zhuǎn)換后的csv文件。

如果需要對json文件中每個字典的key字段進行修改,比如需要將上面dict中的coordinate中的經(jīng)緯度數(shù)據(jù)取出來存為x、y數(shù)據(jù),則可以按照下面的方法(此方法還可以調(diào)整每個屬性顯示的順序,效果更好一點):

import csvimport jsonimport sysimport codecsdef trans(path): jsonData = codecs.open(path+’.json’, ’r’, ’utf-8’) # csvfile = open(path+’.csv’, ’w’) # 此處這樣寫會導(dǎo)致寫出來的文件會有空行 # csvfile = open(path+’.csv’, ’wb’) # python2下 csvfile = open(path+’.csv’, ’w’, newline=’’) # python3下 writer = csv.writer(csvfile, delimiter=’t’, quoting=csv.QUOTE_ALL) keys=[’id’, ’name’, ’category’, ’price’, ’count’, ’type’, ’address’, ’link’, ’x’, ’y’] writer.writerow(keys) i = 1 for dic in jsonData: dic = json.loads(dic[0:-1]) x = dic[’coordinates’][0] y = dic[’coordinates’][1] writer.writerow([str(i),dic[’name’],dic[’category’],dic[’price’],dic[’count’],dic[’type’],dic[’address’],dic[’link’],x,y]) i += 1 jsonData.close() csvfile.close()if __name__ == ’__main__’: path = str(sys.argv[1]) print (path) trans(path)

運行方法同上。

json文件是我在大眾點評抓取的數(shù)據(jù),存儲格式為utf-8。建議使用codecs包來讀取json數(shù)據(jù),可指定編碼方式。

jsonData = codecs.open(path + ’.json’, ’r’, encoding=’utf-8’)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 乌拉特前旗| 慈溪市| 丰宁| 双柏县| 普格县| 扎赉特旗| 磐安县| 屏南县| 即墨市| 和林格尔县| 宜春市| 辽阳县| 娄烦县| 铜川市| 东阿县| 柳江县| 永和县| 登封市| 江西省| 壤塘县| 江油市| 台南县| 同仁县| 吕梁市| 灵台县| 济源市| 大田县| 醴陵市| 郯城县| 苗栗市| 洪湖市| 浦城县| 图木舒克市| 旅游| 西林县| 蛟河市| 阿拉善右旗| 云南省| 永平县| 右玉县| 台南县|