读取CSV文件并返回字典 [英] Reading csv file and returning as dictionary

查看:314
本文介绍了读取CSV文件并返回字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个正确读取文件的函数,但有一些问题。它需要返回作为一个字典,其中的键是艺术家的名字,值是元组列表(不知道这个,但似乎是它的要求)

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

 艺术家,标题,年份,总高度 ,总宽度,媒体,国家
巴勃罗·毕加索,格尔尼卡,1937,349.0,776.0,油画,西班牙 Vincent van Gogh,晚上咖啡厅,1888,81.0,65.5,油画,荷兰
达芬奇,蒙娜丽莎,1503 ,油画颜料,法国
文森特·梵高,绷带耳自画像,1889,51.0,45.0,油画,美国
达芬奇,伊莎贝拉·埃斯特的肖像,1499,63.0,46.0,粉笔,法国
达芬奇,最后的晚餐,1495,460.0,880.0,蛋彩画,意大利

所以我需要读取一个输入文件并将其转换成如下所示的字典:

 <$ c $ (格尔尼卡,1937年,349.0,776.0,油画,西班牙)],
达芬奇:[(蒙娜丽莎,1503,76.8,5 3.0,油画,法国),
(伊莎贝拉·埃斯特的肖像,1499,63.0,46.0,粉笔,法国),
(最后的晚餐,1495,460.0,880.0,tempera,Italy)],
Vincent van Gogh:[(1888年的Cafe Terrace at Night,81.0,65.5,油画颜料,荷兰),
(1889,51.0,45.0,油画,美国)]
} / pre>

我遇到的主要问题是跳过第一行艺术家,标题等,只在第一行之后返回行。我也不确定我现在的代码是否以字典的形式返回。这是我到目前为止

  def convertLines(lines):
head = lines [0]
del line [0]
infoDict = {}
行代码:#遍历除第一行之外的任何内容
infoDict [line.split(,)[0]] = [ tuple(line.split(,)[1:])]
return infoDict
$ b $ def read_file(filename):
thefile = open(filename,r)
lines = []
for thefile:
lines.append(i)
thefile.close()
mydict = convertLines(read_file(filename))
返回行

仅仅对我的代码做一些小的修改就可以返回正确的结果,需要以不同的方式处理?它看起来,我目前的代码读取完整的文件,但我怎么会跳过第一行,并可能返回字典表示如果它不是已经?感谢您的帮助

解决方案

首先我们要删除列表的第一行。然后我们运行一个函数来完成你所说的,用一个元组列表作为值来创建一个字典。

您可以保留已有的函数,并对行变量执行此操作。



好的运行下面的代码,你应该是很好的

  def convertLines(lines):
head = lines [0]
del line [0]
infoDict = {}
行代码:#除了第一行b $ b infoDict [line.split(,)[0]] = [tuple(line.split(,)[1:])]
return infoDict

def read_file(文件名):
thefile =打开(文件名,r)
行= []
在文件中:
lines.append(i)
thefile.close()
返回行

mydict = convertLines(read_file(filename))
print(mydict)


I've written a function that currently reads a file correctly but there are a couple of problems. It needs to be returned as a dictionary where the keys are artist names and the values are lists of tuples (not sure about this but that appears to be what its asking)

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"

So I need to read an input file 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")]
      }

The main problem I'm having is skipping the first line that says "Artist","Title", etc. and only returning the lines after the first line. I'm also not sure if my current code is returning this as a dictionary. Here's what I have so far

def convertLines(lines):
    head = lines[0]
    del lines[0]
    infoDict = {}
    for line in lines: #Going through everything but the first line
        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 but how would I skip the first line and possibly return in dict representation if it isnt already? Thanks for any help

解决方案

First thing we do is delete the first line of the list.

Then we run a function to do exactly as you say, make a dictionary with list of tuples as values.

You can keep the function you have and run this operation on the lines variable.

Alright run the following code and you should be good

def convertLines(lines):
    head = lines[0]
    del lines[0]
    infoDict = {}
    for line in lines: #Going through everything but the first line
        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()
    return lines

mydict = convertLines(read_file(filename))
print(mydict)
#Do what you want with mydict below this line

这篇关于读取CSV文件并返回字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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