简体   繁体   English

Python:如何将字典值解析为2个单独的值,以便我可以导出为.csv?

[英]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中写了以下内容:

  1. Script to scrape Craigslist listings. 脚本来刮取Craigslist列表。 Includes latitude and longitude of each listing. 包括每个列表的纬度和经度。 Saved as .csv file 保存为.csv文件
  2. Script to scrape list of subway stations. 脚本清除地铁站列表。 Includes latitude and longitude of each station. 包括每个站的纬度和经度。 Saved as .csv file 保存为.csv文件
  3. Script that takes these 2 .csv files, and calculates the distance of each pair of coordinates. 获取这两个.csv文件的脚本,并计算每对坐标的距离。 For each listing, find the closest subway station, and the corresponding distance 对于每个列表,找到最近的地铁站,以及相应的距离

`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.

相关问题 如何从字典值中删除[]和'',以便可以在csv文件中正确打印 - How do I remove [ ] and ' ' from my dictionary values so that I can print it properly in a csv file 如何分隔字典的值以便它们可以打印在不同的列中? - How do I separate a dictionary's values so that they can be printed in different columns? 如何将所有变量从项目导出到.csv,以便随后将其导入Python环境? - How do I export all variables from a project to .csv so that I can then import them into a Python environment? 如何在python中获取已解析查询字符串的值? - How do I get the values of a parsed query string in python? 尝试使用 Python 将解析的数据导出到 CSV 文件,但我不知道如何导出超过一行 - Attempting to export parsed data to CSV file with Python and I can't figure out how to export more than one row 如何分隔 Pandas 中的字典值? - How do I separate dictionary values in Pandas? 如何将字典导出到 dataframe? 在 python - How can I export a dictionary to a dataframe? In python 如何从另一个python字典中的一个字典中获得相应的列表值,在列表中它们被列为键,比较并打印出csv? - How can I get corresponding list values in one dictionary from another python dictionary where they are listed as keys, compare and print out a csv? 如何确保我从CSV读取的所有项目都解析为字典? - How do I make sure that all of the items that I read from a CSV are parsed into a dictionary? 如何将python crypt对象转换为csv,以便可以在内容上调用csv方法 - How do I convert python crypt object to csv so I can call csv methods on the contents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM