将csv文件转换为字典 [英] Converting csv file to dictionary

查看:404
本文介绍了将csv文件转换为字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天问了这个问题,但仍然坚持。我写了一个函数,目前正确读取一个文件,但有一些问题。



我遇到的主要问题是,我需要以某种方式跳过文件的第一行,我不知道如果我将它作为字典返回。下面是一个文件的示例:

 Artist,Title年份,总高度,总宽度,媒体,国家
Pablo Picasso,Guernica,1937,349.0,776.0 西班牙
Vincent van Gogh,Cafe Terrace at Night,1888,81.0,65.5,油画,荷兰
Leonardo da Vinci Mona Lisa,1503,76.8,53.0,oil paint,France
Vincent van Gogh,Self-Portrait with Bandaged Ear,1889,51.0 ,45.0,油画,美国
Leonardo da Vinci,Isabella d'Este肖像,1499,63.0,46.0 France
Leonardo da Vinci,The Last Supper,1495,460.0,880.0,tempera,Italy
pre>

我需要读取一个像上面这样的文件,并将其转换为如下所示的字典:

  sample_dict = {
Pablo Picasso:[(Guernica,1937,349.0,776.0,oil paint,Spain)],
达芬奇:[(蒙娜丽莎,1503,76.8,53.0,油画,法国),
(伊莎贝拉肖像,1499,63.0,46.0,粉笔,法国),
(最后的晚餐,1495,460.0,880.0,蛋彩画,意大利)],
Vincent van Gogh在夜间1888,81.0,65.5,油画,荷兰),
(带有耳环的自画像,1889,51.0,45.0,油画,美国
}

这里是我到目前为止。我当前的代码工作,但不会将文件转换成一个字典像上面的例子。感谢任何帮助

  def convertLines(lines):
head = lines [0]
del lines [0]
infoDict = {}
行中的行:
infoDict [line.split(,)[0]] = [tuple(line.split(,) [1:])]
return infoDict

def read_file(filename):
thefile = open(filename,r)
lines = []
for i in thefile:
lines.append(i)
thefile.close()
mydict = convertLines(read_file(filename))
返回行

只是对我的代码几个小的更改返回正确的结果或者我需要不同的方法?看来我的当前代码读取完整的文件。感谢任何帮助



编辑:@Julien它是工作(但不正确),直到我今天早上做了一些修订,现在给出递归错误。

解决方案

您只需要这样:

  def read_file(filename):
with open(filename,r)as thefile:
mydict = convertLines(thefile.readlines()))
return mydict

您当前的函数是无限调用自己...然后修复 convertLines 函数如果需要。


I asked this question yesterday but am still stuck on it. I've written a function that currently reads a file correctly but there are a couple of problems.

The main problem I'm having is that I need to somehow skip the first line of the file and I'm not sure if I'm returning it as a dictionary. Here is an example of one of the files:

"Artist","Title","Year","Total  Height","Total  Width","Media","Country"
"Pablo Picasso","Guernica","1937","349.0","776.0","oil  paint","Spain"
"Vincent van Gogh","Cafe Terrace at Night","1888","81.0","65.5","oil paint","Netherlands"
"Leonardo da Vinci","Mona Lisa","1503","76.8","53.0","oil paint","France"
"Vincent van Gogh","Self-Portrait with Bandaged Ear","1889","51.0","45.0","oil paint","USA"
"Leonardo da Vinci","Portrait of Isabella d'Este","1499","63.0","46.0","chalk","France"
"Leonardo da Vinci","The Last Supper","1495","460.0","880.0","tempera","Italy"

I need to read a file like the one above and convert it into a dictionary that looks like this:

sample_dict = {
        "Pablo Picasso":    [("Guernica", 1937, 349.0,  776.0, "oil paint", "Spain")],
        "Leonardo da Vinci": [("Mona Lisa", 1503, 76.8, 53.0, "oil paint", "France"),
                             ("Portrait of Isabella d'Este", 1499, 63.0, 46.0, "chalk", "France"),
                             ("The Last Supper", 1495, 460.0, 880.0, "tempera", "Italy")],
        "Vincent van Gogh": [("Cafe Terrace at Night", 1888, 81.0, 65.5, "oil paint", "Netherlands"),
                             ("Self-Portrait with Bandaged Ear",1889, 51.0, 45.0, "oil paint", "USA")]
      }

Here's what I have so far. My current code works but does not convert the file into a dictionary like the example above. Thanks for any help

def convertLines(lines):
    head = lines[0]
    del lines[0]
    infoDict = {}
    for line in lines:
        infoDict[line.split(",")[0]] = [tuple(line.split(",")[1:])]
    return infoDict

def read_file(filename):
    thefile = open(filename, "r")
    lines = []
    for i in thefile:
        lines.append(i)
    thefile.close()
    mydict = convertLines(read_file(filename))
    return lines

Would just a couple small changes to my code return the correct result or would I need to approach this differently? It does appear that my current code reads the full file. Thanks for any help

Edit: @Julien It was working (but not correctly) until I made some revisions this morning it is now giving a recursion error.

解决方案

You just want this:

def read_file(filename):
    with open(filename, "r") as thefile:
        mydict = convertLines(thefile.readlines()))
        return mydict

your current function is infinitely calling itself... Then fix your convertLines function if it needs to.

这篇关于将csv文件转换为字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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