求你了,我需要帮助,这就是我迄今为止所取得的成就
csvfile = open('new_df.csv', 'r')
jsonfile = open('new_df.csv'.replace('.csv','.json'), 'w')
jsonfile.write('{"' + 'new_df.csv'.replace('.csv','') + '": [\n') # write json parent of data list
fieldnames = csvfile.readline().replace('\n','').split(',') # get fieldnames from first line of csv
num_lines = sum(1 for line in open('new_df.csv')) - 1 # count total lines in csv minus header row
reader = csv.DictReader(csvfile, fieldnames)
i = 0
for row in reader:
i += 1
json.dump(row, jsonfile)
if i < num_lines:
jsonfile.write(',')
jsonfile.write('\n')
jsonfile.write(']}')
ID Arrival Departure ArrivalDate DepatureDate
1001 New York Holland 2009-09-23 2012-07-23
1301 Florida Germany 2010-10-23 2012-10-11
1401 New York Holland 2009-09-23 2009-09-25
1301 New York Beijing 2009-09-23 2010-09-21
1201 New York Holland 2008-01-01 2009-09-23
1001 Virginia New York 2008-01-01 2009-09-22
1021 New York Holland 2009-09-23 2009-09-25
1001 New York Holland 2009-09-24 2012-07-23
1021 New York Holland 2009-09-26 2012-07-23
1001 New York Holland 2009-09-25 2012-07-23
….... ......... ........ .............. ...........
1001 New York Holland 2012-07-23 2012-07-23
1401 New York Holland 2009-09-25 2009-09-25
1301 New York Beijing 2010-09-21 2010-09-21
1201 New York Holland 2009-09-23 2009-09-23
1001 Virginia New York 2009-09-22 2009-09-22
1021 New York Holland 2009-09-25 2009-09-25
1001 New York Holland 2012-07-23 2012-07-23
1021 New York Holland 2012-07-23 2012-07-23
1001 New York Holland 2012-07-23 2012-07-23
迭代ArrivalDate
并按如下所示追加等价的行,然后得到预期的输出:
i、 e
"{ArrivalDate:" { "Arrival": ID1,...,IDn
"Departure": ID1,...,IDn
}
(我需要在出发日期下检查是否与到达日期匹配,如果匹配,我想将其ID附加到出发日期,否则只列出“到达”
{
“2009-09-23”:
{ “New York”: 1001, 1401, 1301, 1021,
“Holland” : 1021,
“Beijing”: 1301,
}
{ “2010-10-23”:
{“Florida”: 1301,
}
{ “2008-01-01”:
{“New York”: 1201,
“Virginia”: 1001,
}
{“2009-09-24”:
{“New York”: 1001
}
{“2009-09-26”:
{“New York”: 1021
}
{“2009-09-25”:
{“New York”: 1001
“Holland”: 1401
}
{“ 2012-07-23”:
{ “New York”: 1001,
“Holland”: 1001,
假设您想要
json
输出:相关问题 更多 >
编程相关推荐