-1

对于这个问题的糟糕标题,我深表歉意,但是没有代码很难解释。我正在尝试让我的代码执行以下操作;如果排名键是 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,

代码片段:

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

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

4

2 回答 2

5

您的编码可能错误的一些区域:

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

我自己尝试使用 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
于 2013-10-20T17:21:18.253 回答
4

对 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
于 2013-10-20T17:04:37.590 回答