[英]JSON format inside record in csv file - convert to column Python
This is my data.这是我的数据。 Inside the column - 'device' and 'ge.network' store the data as a dict or json format.在列内 - 'device' 和 'ge.network' 将数据存储为 dict 或 json 格式。 I would like to create new columns based on data from that columns, fore example -> new column should be 'browser','browserversion', 'continent' and so on.我想根据来自该列的数据创建新列,例如 -> 新列应该是 'browser'、'browserversion'、'continent' 等等。 I have tried a lot of solutions, but it dosen't work.我已经尝试了很多解决方案,但它不起作用。 enter image description here在此处输入图像描述
DATA数据
,date,device,fullVisitorId,geoNetwork 0,20180420,"{""browser"": ""Chrome"", ""browserVersion"": ""not available in demo dataset"", ""browserSize"": ""not available in demo dataset"", ""operatingSystem"": ""Macintosh""}",3.37108036201195E+018,"{""continent"": ""Americas"", ""subContinent"": ""Northern America"", ""country"": ""United States"", ""region"": ""California""}" 1,20180328,"{""browser"": ""Chrome"", ""browserVersion"": ""not available in demo dataset"", ""browserSize"": ""not available in demo dataset"", ""operatingSystem"": ""Macintosh""}",1.27350339266773E+018,"{""continent"": ""Americas"", ""subContinent"": ""Northern America"", ""country"": ""Canada"", ""region"": ""State of Sao Paulo""}" ,date,device,fullVisitorId,geoNetwork 0,20180420,"{""browser"": ""Chrome"", ""browserVersion"": ""not available in demo dataset", ""browserSize"": ""在演示数据集中不可用"", ""operatingSystem"": ""Macintosh""}",3.37108036201195E+018,"{""continent"": ""Americas", ""subContinent"": ""Northern America"", ""country"": ""United States"", ""region"": ""California""}" 1,20180328,"{""browser"": ""Chrome", "" browserVersion"": ""在演示数据集中不可用"", ""browserSize"": ""在演示数据集中不可用", ""operatingSystem"": ""Macintosh""}",1.27350339266773E+018," {""continent"": ""Americas"", ""subContinent"": ""北美"", ""country"": ""加拿大", ""region"": ""圣保罗州“”}”
A little help how to solve my problem一点帮助如何解决我的问题
enter image description here在此处输入图像描述
On the attached picture first new column name is highlited.在所附图片上,第一个新列名称被突出显示。 It is json file是json文件
import csv
import json
def csv_to_json(csvFilePath, jsonFilePath):
jsonArray = []
# read csv file
with open(csvFilePath, encoding='utf-8') as csvf:
csvReader = csv.DictReader(csvf)
for row in csvReader:
jsonArray.append(row)
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonString = json.dumps(jsonArray, indent=4)
jsonf.write(jsonString)
csvFilePath = r'dane.csv'
jsonFilePath = r'data.json'
csv_to_json(csvFilePath, jsonFilePath)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.