读取csv文件并返回为字典

时间:2016-11-13 01:23:01

标签: python file python-3.x csv dictionary

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

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

"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"

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

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")]
      }

我遇到的主要问题是跳过第一行"艺术家","标题"等等,只返回第一行之后的行。我还不确定我当前的代码是否将其作为字典返回。这是我到目前为止所拥有的

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

对我的代码进行一些小的更改会返回正确的结果还是我需要以不同的方式处理?看来我的当前代码确实读取了整个文件,但是如果它已经不存在,我将如何跳过第一行并可能返回dict表示?谢谢你的帮助

4 个答案:

答案 0 :(得分:1)

我们要做的第一件事就是删除列表的第一行。

然后我们运行一个完全按照你说的做的函数,用元组列表作为值创建一个字典。

您可以保留您拥有的功能并在lines变量上运行此操作。

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

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

答案 1 :(得分:1)

你应该试试这个。我觉得很简单

public Color calculateIlluminationModel(Vector normal, Scene scene)
{
    //c = cr * ca + cr * cl * max(0, n \dot l)) + cl * cp * max(0, e \dot r)^p
    Vector lightSourceColor = getColorVector(scene.getLight().getLightColor()); //cl
    Vector diffuseReflectanceColor = getColorVector(getMaterialColor()); //cr
    Vector ambientColor = getColorVector(scene.getLight().getAmbientLightColor()); //ca
    Vector specularHighlightColor = getColorVector(getSpecularHighlight()); //cp
    Vector directionToLight = scene.getLight().getDirectionToLight(); //l
    Vector reflectionVector = normal.multiply(2).multiply(normal.crossProduct(directionToLight)).subtract(directionToLight); //r = 2n(n \dot l) - l

    Vector ambientTerm = diffuseReflectanceColor.multiply(ambientColor);
    double angleBetweenLightAndNormal = directionToLight.dotProduct(normal);
    Vector diffuseTerm = diffuseReflectanceColor.multiply(lightSourceColor).multiply(Math.max(0, angleBetweenLightAndNormal));
    Vector phongTerm = lightSourceColor.multiply(specularHighlightColor).multiply(Math.pow(Math.max(0, scene.getCameraSettings().getLookFrom().dotProduct(reflectionVector)), (double) getPhongConstant()));
    return getVectorColor(ambientTerm.add(diffuseTerm).add(phongTerm));
}

输出:

import csv
from collections import defaultdict

d_dict = defaultdict(list)
with open('file.txt') as f:
    reader = csv.reader(f)
    reader.next()
    for i in list(reader):
        d_dict[i[0]].append(tuple(i[1:]))

print dict(d_dict)

答案 2 :(得分:0)

更好的方法是:

    with open('filename','r,') as file: # Make a file object
        items = []
        _ = file.readline()  # This will read the first line and store it in _  
                             # a variable of no use. 
        for line in file:    # Next we start the for loop to read all other  
                             # data
            item.append(line)

执行此代码后,with语句将关闭文件对象。所以不需要做f.close()

答案 3 :(得分:0)

csv模块提供了处理CSV文件的有用工具。以下应该做:

import csv
from collections import defaultdict

def read_file(filename):
    with open(filename, 'r') as f:
        reader = csv.DictReader(f, delimiter=',')
        result_dict = defaultdict(list)
        fields = ("Title", "Year", "Total  Height", "Total  Width", "Media", "Country")
        for row in reader:
            result_dict[row['Artist']].append(
                tuple(row[field] for field in fields)
            )
    return dict(result_dict)

DictReader使用文件第一行中的字段作为字段名称。然后它返回一个可迭代的文件中的行,这些行被生成为dicts,字段名称为键。