[英]Python: How do I get the dictionary value to be parsed as 2 separate values so I can export as a .csv?
I am looking to find the closest subway station to a Craigslist apt listing and the distance (in miles) from the listing. 我希望找到离Craigslist apt列表最近的地铁站以及距离列表的距离(以英里为单位)。 I would like to export this as a .csv file for further analysis.
我想将其导出为.csv文件以供进一步分析。
I have written the following in python: 我在python中写了以下内容:
`Code: `代码:
import csv
from geopy.distance import vincenty
from operator import itemgetter
with open('coord.csv') as csvfile:
#skip first line in csv
next(csvfile)
#read csv
readCSV = csv.reader(csvfile, delimiter=',')
#store results in a dictionary
subwayCoords = {}
#loop through each row in csv
for row in readCSV:
subway = row[1]
s_coord = row[0],row[3]
subwayCoords[subway] = s_coord
with open('items.csv') as csvfile:
next(csvfile)
readCSV = csv.reader(csvfile, delimiter=',')
craigCoords = {}
for row in readCSV:
craigID = row[1]
c_coord = row[11]
craigCoords[craigID] = c_coord
craigDist = {} #dictionary: distance between each listing and subway
craigMin = {} #dictionary: nearest subway to each listing
#get each listing's coordinates (key=listing, value=coordinates)
for craigID, c_coord in craigCoords.items():
#get each subway's coordinates (key=subway, value=coordinates)
for subway, s_coord in subwayCoords.items():
#calculate distance between each listing and subway
dist = vincenty(s_coord, c_coord).miles
print "distance between " + ''.join(str(craigID)) + " and " + ''.join(str(subway)) + " = " + str(dist)
craigDist[subway] = dist
#for each listing, calculate closest subway; returns subway, distance as a tuple
minPair = min(craigDist.iteritems(), key=itemgetter(1))
craigMin[craigID] = minPair
print craigMin
#export craigMin dictionary
with open('mycsvfile.csv','wb') as csvfile:
w = csv.writer(csvfile)
w.writerows(craigMin.items())
I now have a dictionary with key, value pairs as follows: 我现在有一个包含键值对的字典,如下所示:
{listing: (closest subway station, distance),...} {上市地址:(最近的地铁站,距离),...}
Run-time output: 运行时输出:
{
'6022151897': ('Kew Gardens\xe2\x80\x93Union Turnpike (IND Queens Boulevard Line)', 1.1243919326522678),
'6022258759': ('Forest Hills\xe2\x80\x9371st Avenue (IND Queens Boulevard Line)', 0.20148597888760844),
'6022892363': ('Vernon Boulevard\xe2\x80\x93Jackson Avenue (IRT Flushing Line)', 0.37261054608700767)
}
.csv output: .csv输出:
6022151897,"('Kew Gardens\xe2\x80\x93Union Turnpike (IND Queens Boulevard Line)', 1.1243919326522678)"
6022258759,"('Forest Hills\xe2\x80\x9371st Avenue (IND Queens Boulevard Line)', 0.20148597888760844)"
6022892363,"('Vernon Boulevard\xe2\x80\x93Jackson Avenue (IRT Flushing Line)', 0.37261054608700767)"
Note that the value contains 2 values, instead of one. 请注意,该值包含2个值,而不是1个值。
How do I get the value to be parsed as 2 separate values so I can export as a .csv? 如何将值解析为2个单独的值,以便我可以导出为.csv? Any other tips for making the script more efficient would also be appreciated.
任何其他提高脚本效率的技巧也将受到赞赏。
try this, it should flatten it and write your csv: 试试这个,它应该压扁它并写下你的csv:
with open('mycsvfile.csv','wb') as csvfile:
w = csv.writer(csvfile)
for key, value in craigMin.items():
w.writerows([key, value[0], value[1]])
Idea being, you have to pull the item in the dictionary apart. 想法,你必须将字典中的项目分开。
Output of this approach: 这种方法的输出:
6022151897, Kew Gardens–Union Turnpike (IND Queens Boulevard Line), 1.124391933
After I parsed out your comment you said this: 在我解析你的评论后你说:
I tried this, and the output of my .csv looks the same:
我试过这个,我的.csv的输出看起来是一样的:
6022151897,"('Kew Gardens\xe2\x80\x93Union Turnpike (IND Queens Boulevard Line)', 1.1243919326522678)"
6022258759,"('Forest Hills\xe2\x80\x9371st Avenue (IND Queens Boulevard Line)', 0.20148597888760844)"
6022892363,"('Vernon Boulevard\xe2\x80\x93Jackson Avenue (IRT Flushing Line)',
0.37261054608700767)"
What I am looking for is clean text in value[0] and value[1].
我正在寻找的是值[0]和值[1]中的干净文本。 For example, value[0] = Kew Gardens\\xe2\\x80\\x93Union Turnpike (IND Queens Boulevard Line. No extra () or " or '. Likewise, value[1] = 1.1243919326522678
例如,值[0] = Kew Gardens \\ xe2 \\ x80 \\ x93Union Turnpike(IND Queens Boulevard Line。无额外()或“或”。同样,值[1] = 1.1243919326522678
What I am trying to convey is that the approach I gave you is very different than the code you have in your question. 我要传达的是,我给你的方法与你在问题中的代码非常不同。 There's an entire loop that unpacks the dictionary instead of just shoving the .items() tuple into the csv.
有一个完整的循环解压缩字典而不是将.items()元组推入csv。
I ran your code and got your result, you can't be doing what I suggested if thats the output you're getting, as far as I can tell. 我运行你的代码得到了你的结果,如果那是你得到的输出,你不能做我建议的,据我所知。
Can you post your approach where you say "I tried this..." 你能说出“我试过这个......”的方法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.