如何使用 python 更改从 csv 调用的返回数据格式

How to Alter returned data format called from csv using python

我正在寻找有关来自 csv 文件的 return 结果格式的指导。到目前为止,我必须使用的代码部分实现了我的 objective,但尽管通过此代码和许多其他 sites/forums 进行了大量研究,但我无法解决最后一步。我也在 gis.stackexchange 上提出了这个问题,但被重定向到这个论坛,评论是 "Questions relating to general Information Technology, with no clear GIS component, are off-topic here, but can be researched/asked at Stack Overflow".

我成功的 python 从 csv 中读取选定数据并 return 以 dict 格式读取数据的代码如下; (是的,我知道它 returns 作为 dict 类型的原因是由于我的代码调用的格式!!!这就​​是问题的关键)

import arcpy, csv
Att_Dict ={}
with open ("C:/Data/Code/Python/Library/Peter/123.csv") as f:
    reader = csv.DictReader(f)
    for row in reader:
        if row['Status']=='Keep':
            Att_Dict.update({row['book_id']:row['book_ref']})  
print Att_Dict 

Att_Dict = {'7643': '7625', '9644': '2289', '4406': '4443', '7588': '9681', '2252': '7947'}

对于 运行 代码的下一部分,我需要上面的结果,但格式为 ; (这是一个非常冗长的代码的一部分,但唯一的阻碍是 returned 格式,因此在发布其他 200 多行代码时价值不大)

Att_Dict = [[7643, 7625], [9644, 2289], [4406, 4443], [7588, 9681], [2252, 7947]]

虽然我已经进行了无数次试验并且可以通过恢复到 csv.Reader 而不是 csv.DictReader 来实现这一点,但我随后失去了 'weed out' 列 'Status' 具有的行的能力其中的值 'Keep',这是手头任务的要求。

迄今为止,我的大锤方法是在 Idle 中使用 'search and replace' 来修改 returned 集以满足其他要求,但我确信它可以通过编程而不是手动完成。与 https://docs.python.org/2/library/index.html, plus my startout question at and Using Python's csv.dictreader to search for specific key to then print its value 相似但不完全相同,另外还有 geonet.esri.

处的大量基于 csv 的问题

(使用 Win 7, ArcGIS 10.2, Python 2.7.5)

试试这个

Att_Dict = {'7643': '7625', '9644': '2289', '4406': '4443', '7588': '9681', '2252': '7947'}
Att_List = []
for key, value in Att_Dict.items():   
    Att_List.append([int(key), int(value)])

print Att_List

Out: [[7643, 7625], [9644, 2289], [4406, 4443], [7588, 9681], [2252, 7947]]