使用CSV控制python脚本

时间:2018-06-22 14:57:41

标签: python csv variables

我想用csv中的值控制我的python脚本输出: 我选择一个类别和一些类似的参数: csv1 或类似csv2

在我的python脚本中,我在看起来像这样的列表中获得了每个参数的值:

A_Names = ("Anton", "Berta", "Charlie")
Ages = ("32","18","23")
Nicknames = ("Agent A", "Agent B", None)
Birthdays = ("03.03.1986", "02.02.2000", "01.01.1995")
IDs = (100, 200, 300)

现在的想法是拥有一个将这些信息组合在一起的脚本,因此csv1的输出将是:

Anton
   A_Name: Anton
   Age: 32
   Nickname: Agent A
   Birthday: 03.03.1986
Berta
   A_Name: Berta
   Age: 18
   Nickname: Agent B
   Birthday: 02.02.2000
Charlie
   A_Name: Charlie
   Age: 23
   Nickname: No Nickname yet
   Birthday: 01.01.1995

csv2的输出为:

Anton
   Nickname: Agent A
   A_Name: Anton
   ID: 100
Berta
   Nickname: Agent B
   A_Name: Berta
   ID: 200
Charlie
   Nickname: No Nickname yet
   A_Name: Charlie
   ID: 300

当然,它也应该像这样在csv3中一起工作,并且相同的输出紧随其后。

所以第一个想法就是这样编码我的输出:

#let's assume i got the columns from the csv in lists like that:
Categories = ("Agents", "Agents")
Parameter1 = ("A_Name", "Nickname")
Parameter2 = ("Age", "A_Name")
Parameter3 = ("Nickname", "ID")
Parameter4 = ("Birthday")

#then the most unflexible code would look like that:

def Show_Values():
    for n in range (0,len(A_Names)):
        print A_Names[n]
        print "  %s: %s" % (Parameter1[0], A_Names[n])
        print "  %s: %s" % (Parameter2[0], Ages[n])
        print "  %s: %s" % (Parameter3[0], Nicknames[n])
        print "  %s: %s" % (Parameter4[0], Birthdays[n])

    for n in range (0,len(A_Names)):
        print A_Names[n]
        print "  %s: %s" % (Parameter1[1], Nicknames[n])
        print "  %s: %s" % (Parameter2[1], A_Names[n])
        print "  %s: %s" % (Parameter3[1], IDs[n])

Show_Values()

所以这是非常愚蠢的代码,我一遍又一遍地重复自己,总是需要为第二个%s添加正确的值。 现在我的问题是如何创建更智能,更短的代码,该代码知道A_Name [0]是A_Names的第一个元素。 并且他应该自动添加“ %s%s”%(参数,值),以获取我要通过csv查找的参数数量。我想我需要 该行的长度,因此来自csv的另外两个列表:

Row0 = (Agents, A_Name, Age, Nickname, Birthday)
Row1 = (Agents, Nickname, A_Name, ID)
len(Row0)-1
len(Row1)-1

编辑:

好的,到目前为止,谢谢,这是我的新方法,仅从csv中读取行:

import csv

with open("TableAgentsComma.csv", "rb") as file:
    reader = csv.reader(file, delimiter=",")
    inputHeader = next(reader)
    rows=[r for r in reader]


A_Names = ["Anton", "Berta", "Charlie"]
Ages = ["32","18","23"]
Nicknames = ["Agent A", "Agent B", None]
Birthdays = ["03.03.1986", "02.02.2000", "01.01.1995"]
IDs = [100, 200, 300]

def Show_Values():
    for n in range (0, len(A_Names)):
        print A_Names[n]
        print "  %s: %s" % (rows[0][1], A_Names[n])
        print "  %s: %s" % (rows[0][2], Ages[n])
        print "  %s: %s" % (rows[0][3], Nicknames[n])
        print "  %s: %s" % (rows[0][4], Birthdays[n])
    for n in range (0, len(A_Names)):
        print A_Names[n]
        print "  %s: %s" % (rows[1][1], Nicknames[n])
        print "  %s: %s" % (rows[1][2], A_Names[n])
        print "  %s: %s" % (rows[1][3], IDs[n])

Show_Values()

但是它当然仍然是一个不灵活的脚本,我希望它响应csv,并根据行数和内部参数生成其输出。 另外,我还需要找出脚本如何知道在列表ID等中找到参数“ ID”。

csv文件:

Category,Parameter1,Parameter2,Parameter3,Parameter4,Parameter5,
Agents,A_Name,Age,Nickname,Birthday,,
Agents,Nickname,A_Name,ID,,,
Agents,A_Name,Age,,,,
Agents,ID,,,,,

1 个答案:

答案 0 :(得分:0)

如果您将数据存储为字典,则可以根据csv文件中的名称查找密钥:

data = {
    "A_Names" : ["Anton", "Berta", "Charlie"],
    "Ages" : ["32","18","23"],
    "Nicknames":  ["Agent A", "Agent B", None],
    "Birthdays": ["03.03.1986", "02.02.2000", "01.01.1995"],
    "IDs": [100, 200, 300]
}

def Show_Values():
    for row in rows:
        for n in range (0, len(data["A_Names"])):
            print data["A_Names"][n]
            for header in row:
                if header == "Agents" or header == "": # This field doesn't match any thing in the data.
                    continue
                header = header.strip()
                header += "s"
                print "  %s: %s" % (header, data[header][n]) 

由于数据现在位于dict中,因此我们可以基于从文件中获取的参数来查找值。我可能应该将其称为“参数”而不是“标题”。

Show_Values()

输出:

Anton
  A_Names: Anton
  Ages: 32
  Nicknames: Agent A
  Birthdays: 03.03.1986
Berta
  A_Names: Berta
  Ages: 18
  Nicknames: Agent B
  Birthdays: 02.02.2000
Charlie
  A_Names: Charlie
  Ages: 23
  Nicknames: None
  Birthdays: 01.01.1995

然后从.csv的第二行开始:

Anton
  Nicknames: Agent A
  A_Names: Anton
  IDs: 100
Berta
  Nicknames: Agent B
  A_Names: Berta
  IDs: 200
Charlie
  Nicknames: None
  A_Names: Charlie
  IDs: 300