使用Python Dictionary进行表/数据操作

时间:2014-05-30 19:05:20

标签: python csv dictionary key

我需要帮助来完成这个python脚本。我是一家公司的实习生,这是我的第一周。我被要求开发一个python脚本,它将获取.csv并将任何相关列放入(追加)到一列中,这样它们只有15个左右的必要列,其中包含数据。例如,如果有zip4,zip5或邮政编码列,他们希望这些列都位于邮政编码列的下方。

我刚刚开始学习python,因为我正在做这个项目所以请原谅我的noobish问题和词汇。我不是在找你们为我做这件事。我只是在寻找一些指导。事实上,我想了解更多有关python的知识,所以任何能够引导我朝正确方向前进的人都请帮忙。

我使用字典键和值。键是第一行中的每一列。每个键的值是剩余的行(从第二到3000)。现在,我只得到一个关键:价值对。我只将最后一行作为我的数组值,而我只得到一个键。此外,我收到了KeyError消息,因此我的密钥无法正确识别。到目前为止我的代码都在下面。我会继续努力,任何帮助都非常感激!希望我可以帮助我喝啤酒的人,我可以稍微挑选他们的大脑:)

感谢您的时间

# To be able to read csv formated files, we will frist have to import the csv module
import csv

# cols = line.split(',')# each column is split by a comma
#read the file
CSVreader = csv.reader(open('N:/Individual Files/Jerry/2013 customer list qc, cr, db, gb 9-19-2013_JerrysMessingWithVersion.csv', 'rb'), delimiter=',', quotechar='"')

# define open dictionary
SLSDictionary={}# no empty dictionary. Need column names to compare to. 


i=0
#top row are your keys. All other rows are your values

#adjust loop
for row in CSVreader:
# mulitple loops needed here
    if i == 0:
            key = row[i]
    else:
            [values] = [row[1:]]
            SLSDictionary = dict({key: [values]}) # Dictionary is keys and array of values
    i=i+1


#print Dictionary to check errors and make sure dictionary is filled with keys and values        
print SLSDictionary

# SLSDictionary has key of zip/phone plus any characters
#SLSDictionary.has_key('zip.+')
SLSDictionary.has_key('phone.+')

#value of key are set equal to x. Values of that column set equal to x
#[x]=value

#IF SLSDictionary has the key of zip plus any characters, move values to zip key
#if true:   
#        SLSDictionary['zip'].append([x])
    #SLSDictionary['phone_home'].append([value]) # I need to append the values of the specific column, not all columns
    #move key's values  to correct, corresponding key
SLSDictionary['phone_home'].append(SLSDictionary[has_key('phone.+')])#Append the values of the key/column 'phone plus characters' to phone_home key/column in SLSDictionary
#if false:
#        print ''
    # go to next key

SLSDictionary.has_value('')

if true:
    print 'Error: No data in column'

# if there's no data in rows 1-?. Delete column
#if value <= 0:
#        del column

print SLSDictionary 

1 个答案:

答案 0 :(得分:0)

发现了几个错误,只是快速查看它。您需要注意的一件事是,您每次都要为现有字典分配新值:

SLSDictionary = dict({key: [values]})

每次进入该循环时,您都会为SLSDictionary重新分配一个新值。因此,最后您只有最底层的条目。要向字典添加密钥,请执行以下操作:

SLSDictionary[key] = values

此外,您不应该需要此行中的括号:

[values] = [row[1:]]

应该只是:

values = row[1:]

但最重要的是,你只会拥有一把钥匙,因为你不断增加你的i值。所以它只会有一把钥匙,所有东西都会不断分配给它。如果没有CSV看起来的样本,我就无法指导您如何重构循环以便它能够捕获所有密钥。

假设您的CSV与您所说的一样:

Col1, Col2, Col3, Col4
Val1, Val2, Val3, Val4
Val11, Val22, Val33, Val44
Val111, Val222, Val333, Val444

然后你可能想要这样的东西:

dummy = [["col1", "col2", "col3", "col4"],
         ["val1", "val2", "val3", "val4"],
         ["val11", "val22", "val33", "val44"],
         ["val111", "val222", "val333", "val444"]]

column_index = []
SLSDictionary = {}

for each in dummy[0]:
    column_index.append(each)
    SLSDictionary[each] = []

for each in dummy[1:]:
    for i, every in enumerate(each):
        try:
            if column_index[i] in SLSDictionary.keys():
                SLSDictionary[column_index[i]].append(every)
        except:
            pass

print SLSDictionary

哪种收益......

{'col4': ['val4', 'val44', 'val444'], 'col2': ['val2', 'val22', 'val222'], 'col3': ['val3', 'val33', 'val333'], 'col1': ['val1', 'val11', 'val111']}

如果您希望它们按顺序排列,请将字典类型更改为OrderedDict()

相关问题