Python 从csv到json的数据
我试图创建一个程序,将CSV文件转换为具有特定布局的JSON文件 输入(CSV) “名字”、“姓氏”、“电子邮件”、“订单总数”、“花费总额”、“平均值” 订单价值、上次订单日期、客户日期、发货日期 名称、发货地址1、发货地址2、发货 城市“,”航运省/州“,”航运邮编“,”航运 “国家”、“发货电话号码”、“账单名称”、“账单地址” 1、“开票地址2”、“开票城市”、“开票地点” 省/州“,”计费邮政编码“,”计费国家“,”计费电话 编号、标签、测试、测试test@hotmail.com","1","19.95","19.95","2016-10-06 11:48:02-0400,2016-10-06 11:48:02-0400,测试 测试“,”测试“,”测试“,”测试“,”测试“,”测试“,”测试“,”测试“,”测试 测试“,”测试“,”测试“,”测试“,”测试“,”测试“,” 输出(JSON)Python 从csv到json的数据,python,json,csv,Python,Json,Csv,我试图创建一个程序,将CSV文件转换为具有特定布局的JSON文件 输入(CSV) “名字”、“姓氏”、“电子邮件”、“订单总数”、“花费总额”、“平均值” 订单价值、上次订单日期、客户日期、发货日期 名称、发货地址1、发货地址2、发货 城市“,”航运省/州“,”航运邮编“,”航运 “国家”、“发货电话号码”、“账单名称”、“账单地址” 1、“开票地址2”、“开票城市”、“开票地点” 省/州“,”计费邮政编码“,”计费国家“,”计费电话 编号、标签、测试、测试test@hotmail.com","
这就是我想要得到的,但我还没有接近。我只能获取所有数据,但我不知道如何将其过滤掉,并保持部分不变 也许这会对你有所帮助
import json
import csv
csvfile = open('a.csv', 'r')
reader = csv.reader(csvfile, delimiter=',')
reader = csv.DictReader(csvfile, next(reader))
result = {'data': {}}
for row in reader:
shipping = {}
billing = {}
customer = {}
for key, value in row.iteritems():
if 'Shipping' in key:
shipping[key] = value
elif 'Billing' in key:
billing[key] = value
else:
customer[key] = value
result['data']['Customer'] = [customer]
result['data']['Shipping'] = [shipping]
result['data']['Billing'] = [billing]
print json.dumps(result, sort_keys=True, indent=4)
结果:
{
"data": {
"Billing": [
{
"Billing Address 1": "test test",
"Billing Address 2": "",
"Billing City": "test",
"Billing Country": "test",
"Billing Name": "test",
"Billing Phone Number": "",
"Billing Province/State": "",
"Billing Zip": "test"
}
],
"Customer": [
{
"Average OrderValue": "19.95",
"Customer Since": "2016-10-06 11:48",
"Date of Last Order": "2016-10-06 11:48",
"Email": "test@hotmail.com",
"First Name": "test",
"Last Name": "test",
"Tags": "",
"Total Orders": "1",
"Total Spent": "19.95"
}
],
"Shipping": [
{
"Shipping Address 1": "test test",
"Shipping Address 2": "",
"Shipping City": "test",
"Shipping Country": "test",
"Shipping Name": "test",
"Shipping Phone Number": "",
"Shipping Province/State": "",
"Shipping Zip": "test"
}
]
}
}
那么,你尝试过什么,确切的问题在哪里呢。向我们展示你的代码。你必须展示你的尝试,你不能只是寻求一般帮助。你能把CSV文件上传到某个地方吗?非常感谢,这正是我想要的。
{
"data": {
"Billing": [
{
"Billing Address 1": "test test",
"Billing Address 2": "",
"Billing City": "test",
"Billing Country": "test",
"Billing Name": "test",
"Billing Phone Number": "",
"Billing Province/State": "",
"Billing Zip": "test"
}
],
"Customer": [
{
"Average OrderValue": "19.95",
"Customer Since": "2016-10-06 11:48",
"Date of Last Order": "2016-10-06 11:48",
"Email": "test@hotmail.com",
"First Name": "test",
"Last Name": "test",
"Tags": "",
"Total Orders": "1",
"Total Spent": "19.95"
}
],
"Shipping": [
{
"Shipping Address 1": "test test",
"Shipping Address 2": "",
"Shipping City": "test",
"Shipping Country": "test",
"Shipping Name": "test",
"Shipping Phone Number": "",
"Shipping Province/State": "",
"Shipping Zip": "test"
}
]
}
}