In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Python how to convert csv format into JSON format file". In daily operation, I believe many people have doubts about how to convert csv format into JSON format file in Python. Xiaobian consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "Python how to convert csv format into JSON format file". Next, please follow the editor to study!
The csv file is as follows:
1 Twin Oaks Place 10 Marquette Rd. 12 Craven Way 12 Fort Sheriden Ave. 12 Skokie Valley Rd. 12 Walker Ave. 120 high St.
First, use built-in functions to handle
# / usr/bin/env python#-*-coding:utf-8-*-import sysimport jsonreload (sys) sys.setdefaultencoding ('utf-8') # depending on whether the list is empty or not Update the matching key-value pairs that are not empty to the dictionary def list_name (keyname, value1, dict1=None): dict1= dict (zip (keyname, value1)) return dict1with open (rusted:\ address.csv') 'rb') as f: for line in f: if line = = []: line = "" else: if line [- 1] = = "\ n": line = line [:-1] if line [- 1] = "\ r": line = line [:-1] akk = [y for y in line.split (")] key1 = ['street' 'namefirst','namelast','address'] A1 = {} arr = list_name (key1,akk,a1) arr = json.dumps (arr) print arr
The output is as follows:
{"namelast": "Oaks", "street": "1", "namefirst": "Twin", "address": "Place"} {"namelast": "Rd.", "street": "10", "namefirst": "Marquette"} {"namelast": "Way", "street": "12", "namefirst": "Craven"} {"namelast": "Sheriden", "street": "12", "namefirst": "Fort" "address": "Ave."} {"namelast": "Valley", "street": "12", "namefirst": "Skokie", "address": "Rd."} {"namelast": "Ave.", "street": "12", "namefirst": "Walker"} {"namelast": "St.", "street": "120", "namefirst": "high"}
Second, define the function yourself, and the content is controllable.
# / usr/bin/env python#-*-coding:utf-8-*-import sysimport jsonreload (sys) sys.setdefaultencoding ('utf-8') # depending on whether the list is empty or not Update the matching key-value pairs that are not empty to the dictionary def list_name (keyname, value1, dict1=None): for i in range (0, len (value1)): if value1 [I] = "": break else: dit = {keyname [I]: value1 [I]} dict1.update (dit) I + = 1 Return dict1with open (rusted D:\ address.csv' 'rb') as f: for line in f: if line = = []: line = "" else: if line [- 1] = = "\ n": line = line [:-1] if line [- 1] = "\ r": line = line [:-1] akk = [y for y in line.split (")] key1 = ['street' 'namefirst','namelast','address'] A1 = {} arr = list_name (key1,akk,a1) arr = json.dumps (arr) print arr
The output is as follows:
{"namelast": "Oaks", "street": "1", "namefirst": "Twin", "address": "Place"} {"namelast": "Rd.", "street": "10", "namefirst": "Marquette"} {"namelast": "Way", "street": "12", "namefirst": "Craven"} {"namelast": "Sheriden", "street": "12", "namefirst": "Fort" "address": "Ave."} {"namelast": "Valley", "street": "12", "namefirst": "Skokie", "address": "Rd."} {"namelast": "Ave.", "street": "12", "namefirst": "Walker"} {"namelast": "St.", "street": "120", "namefirst": "high"} so far. On the "Python how to convert csv format to JSON format file" study is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.