如何将列从csvfile转换为orderedDict Python [英] How to convert the columns from a csvfile into an orderedDict python

查看:328
本文介绍了如何将列从csvfile转换为orderedDict Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件,我需要列打印为OrderedDict



我可以将行转换成一个ordereddict使用
collections.OrderedDict(row [0],row [1:])for row in r)在python(2.7.5)



但是当我尝试相同的列,我得到<无法解压缩多个值'错误。



有什么解决方法吗?

  fileLocation ='C:/test.csv'
with open ,'rb')as f:
r = csv.reader(f)
od = collections.OrderedDict(row [0],row [1:])for row in r)
print od


解决方案

尝试使用此



od = collections.OrderedDict(row [0],row [1:])for row in r if len(row)> 1)

这可能是 row code>一列


I have a csv file and i need the columns to be printed as OrderedDict

I am able to convert the rows into an ordereddict using collections.OrderedDict((row[0], row[1:]) for row in r) in python (2.7.5)

But when i try the same for columns i am getting 'cannot unpack more than one value' error.

Is there any workaround?

        fileLocation = 'C:/test.csv'
        with open(fileLocation,'rb') as f:
            r = csv.reader(f)
            od = collections.OrderedDict((row[0], row[1:]) for row in r)
        print od

解决方案

try using this

od = collections.OrderedDict((row[0], row[1:]) for row in r if len(row)>1)

this might be you have row with only one column

这篇关于如何将列从csvfile转换为orderedDict Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆