【问题标题】:Editing Key value in dictionary of a csv in order to perform the following编辑 csv 字典中的键值以执行以下操作
【发布时间】:2013-10-28 14:07:11
【问题描述】:

对于这个问题的糟糕标题,我深表歉意,但是没有代码很难解释。我正在尝试让我的代码执行以下操作;如果排名键是 2 并且数字键是任何东西(空白、数字或其他),并且如果上一行是排名键 1 和数字键 1,则继续执行脚本的下一行。

但是,尽管付出了很多努力,但我似乎无法让代码产生应有的效果。需要注意的代码行是第 14 行,但我认为最好提供一些背景代码来设置上下文。

示例数据

Unique Name,Rank,100% OmNTS,Numeric
xyz,1,10.2,1
xyz,2,12.5,-
xyz,3,18.8,2
xyz,4,25.9,
pmz,1,29.8,1
pmz,2,200.9,5
pmz,3,210.9,
pmz,4,250.89,
pmz,5,290.9685,
xyz2,1,10.9,1
xyz2,2,12.59,3
xyz2,3,18.8,2
xyz2,4,25.9,,
pmz2,1,29.8,,
pmz2,2,200.9,5
pmz2,3,210.9,
pmz2,4,250.89,
pmz2,5,290.9685,

代码sn-p:

for counter, row in enumerate(reader):
counter += 1
if counter != 1:
    for field in fields[3:]:
        if field == "Numeric" or field == "Rank":
            row[field] = row[field] and float(row[field])
        if field == "Rank":
            row[field] = row[field] and int(row[field])

    key = (row['Rank'], row['Numeric'])
    previous = entries[row['Unique Name']]


    if (key[0] == 2) and (1 <= key[1] <= 50 or key[1] is None) and ((1, 1) in previous):
        p = previous[(1, 1)]
        print 'test data text {r[Rank]} test {p[Rank]} test {r[100% OmNTS]} test {p[100% OmNTS]} '.format(r=row, p=p)

我们希望在第 2 行中进行匹配,其中第 3 行中的元素(包括计数中的标题行)将第 2 行的元素存储为先前的元素。第 7 行和第 12 行也是如此,但是此 Key 引用将忽略第 16 行,因为第 15 行不是 (1,1)

所需的输出

test data text 2 test 1 test 12.5 test 10.2
test data text 2 test 1 test 200.9 test 29.8
test data text 2 test 1 test 12.59 test 10.9

非常感谢您花时间查看我的问题。 原子能机构

【问题讨论】:

  • 首先你应该更正缩进。除了该示例输入之外,这是一个不错的补充。
  • 你能告诉我们到底出了什么问题吗?你有错误吗?是不是进入了if语句?总是输入 if 语句?
  • 在我们说话的时候做一个简单的例子。
  • 有一个答案,但它已被删除我会在完成示例之前快速尝试建议的修复。
  • 它是我的,在我发布后我认为它是错误的(我很惊讶1&lt;None 没有引发错误)。

标签: python python-2.7 csv dictionary key


【解决方案1】:

您的编码可能有误的一些地方:

  • 如果您有一些代码应该打印正确的输出但没有,则可能是 csv 阅读器或文件对象迭代器已用尽。如果您在交互式 shell 中工作,您应该在每次运行循环之前打开文件并初始化读取器对象。
  • 空字段由空字符串表示,而不是由None
  • 从您的代码 sn-p 中不清楚 readerfieldsentries 是什么,也许其中也有一些错误。

我自己尝试使用 csv 模块:

from csv import DictReader

with open('example.csv') as f:
    check = False
    for row in DictReader(f):
        n = row['Numeric'].replace('-', '')
        r = row['Rank']

        if check and r == '2' and (not n or 1 <= float(n) <= 50):
            s = row['100% OmNTS'], omnts_p
            print 'test data text 2 test 1 test %s test %s' % s

        elif r == n == '1':
            check = True
            omnts_p = row['100% OmNTS']

        else:
            check = False

我希望这是你想要的。它确实打印了所需的输出:

test data text 2 test 1 test 12.5 test 10.2
test data text 2 test 1 test 200.9 test 29.8
test data text 2 test 1 test 12.59 test 10.9

【讨论】:

    【解决方案2】:

    对 udnerstand 有点困惑,但我试了一下。

    f = open('data.txt')
    
    # Will hold previous loop's values
    tmp = []
    
    # Start the loop
    for line in f:
        list = line.split(',')
    
        name = list[0]
        rank = list[1]
        omnts = list[2]
        numeric = list[3].strip() # remove newlines
    
        if numeric and rank == '2' and  tmp:
            if tmp[1] == '1' and tmp[3] == '1':
                print "test data text {} test {} test {} test {}".format(rank, tmp[0], omnts, tmp[2])
    
        # Store this loop's values so we can check in the next iteration
        tmp = [name, rank, omnts, numeric]
    

    输出:

    test data text 2 test 1 test 12.5 test 10.2
    test data text 2 test 1 test 200.9 test 29.8
    test data text 2 test 1 test 12.59 test 10.9
    

    【讨论】:

    • ps:这里假设 data.txt 文件不包含行“Unique Name,Rank,100% OmNTS,Numeric”
    猜你喜欢
    • 2023-01-21
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    相关资源
    最近更新 更多