【问题标题】:List index out of Range python2.7 looping based on first value基于第一个值的列表索引超出范围python2.7循环
【发布时间】:2020-05-13 08:40:49
【问题描述】:

美好的一天可能有人请帮助我尝试基于第一个值“token”循环遍历 txt 文件,它当前仅在我指定令牌时循环,例如 {600001130260} 文件中有多个令牌在第一个row[0] id 喜欢它来迭代每个标记/行并提取指定的信息。

数据文件如下所示

600001130260|005|||IN|2197|01||20160905210028
600001130260|100|005|00|VAT|VAT|VAT @ 14%|2,150.14
600001130260|100|013|00|TOT|CTOT|Total Due|86,578.93
600001130260|100|014|00|DD|DD|Due Date|2015/09/22|2015/10/15
600001130260|200|019|01||YDACON|Daily average consumption 79.325 kWh||
28002385859|000|||||LT|||T0IQ04960000000016400000000000000||
28002385859|100|005|00|CUR|CUR|Current Charges (Excl. VAT)|304.48
28002385859|100|006|00|VAT|VAT|VAT @ 14%|10.62
28002385859|100|013|00|TOT|CTOT|Total Due|26,451.75
28002385859|100|014|00|DD|DD|Due Date|2015/09/23
28002385859|150||23,149.02|1,686.37|1,233.57|382.79|0.00|26,451.75

这是我的代码

file1 = open(r"C:\Users\isaac.gumbi\Documents\jhb\Full test file.txt", 'r')
file2 = ""
with file1 as f:
    for line in f:
        tokens = line.split("|")

        keys = {'600001130260','118002191517','CTOT', 'CUR', 'Due Date', 
               'VAT', '020', '030', '010', '040', 'STOT', '000', '005',
                '050', '0100', 'BBF', 'INT','CIN', 'CTOT', 'DD', 'YVLEVY', 
                'YRREM'}

        if len(tokens) and tokens[0] in keys and tokens[5] == 'CTOT':
            Total_due = ' '.join(tokens[7:8])
            if Total_due == '' : Total_Due = "null"
            print ("Total_due", Total_due)

这是我当前的输出

('Total_due', '86,578.93\n')
('Total_due', '79,191.18\n')

我希望它给我输出 total_due 而无需我在 [0] 中指定令牌

【问题讨论】:

  • “我希望它在不指定tocken[0] in keys 的情况下给我输出total_due” - 那么检查token[0] in keys 的目的是什么?应该使用哪些替代标准来确定应该计算 Total_Due

标签: python python-2.7 loops if-statement


【解决方案1】:

你想要Total Due 的所有值吗? 如果是这样,您可以简单地执行以下操作:

sep = "|"
value_name = "Total Due"
result = []

with open("thefile.txt", 'r') as f:
    for line in f:
        tokens = line.split(sep)
        try:
            ind_total_due = tokens.index(value_name) + 1
            result.append((value_name, tokens[ind_total_due]))
        except ValueError:
            continue

结果将是:

[('Total Due', '86,578.93'),
 ('Total Due', '26,451.75'),
 ('Total Due', '3,483.28'),
 ('Total Due', '983.04'),
 ('Total Due', '- 197,358.33')]

第一个“令牌”似乎是一个唯一标识符。 如果您想要 csv 导出和多列支持,您可以这样做:

token_sep = "|"
csv_sep = ";"

# Lambda function that whill format total due
float_formater = lambda string : float(
    string.replace(" ", "").replace(",", "")
)

# Attributes you want to parse
col_names = (
    ("Total Due", float_formater, 1),
    ("Due Date", None, 1),
)

# Python dictionary which associate to each identifier, a total due
# and a due date
records = {}

with open("thefile.txt", 'r') as f:
    for line in f:

        tokens = line.strip().split(token_sep)

        # We assume the first token is an identifier
        unique_id = tokens[0]

        # For each new identifier we create a new record and initialize 
        # total due and due date to an empty string
        if unique_id and unique_id not in records:
            records[unique_id] = {col_name: "" for col_name, _ in col_names}

        # then we look for values we are interesting in. If we find one, we can
        # update one value of the record
        for col_name, formatter, index_val in col_names:
            try:
                ind_col = tokens.index(col_name) + index_val 
                value = tokens[ind_col]

                if formatter:
                    value = formatter(value)

                records[unique_id][col_name] = value


            except ValueError:
                continue

# For an easier csv export we reformat the record dict to a list of values
list_values = [
    (unique_id,) + tuple((values[col] for col, _ in col_names))
    for unique_id, values in records.items()
]

# We can then easily write all the records one by one
with open("mycsv.csv", "w") as f:
    f.write(csv_sep.join(["id"] + [c for c, _ in col_names]))
    for values in list_values:
        print(values)
        f.write("\n")
        f.write(csv_sep.join(map(str, values)))

mycsv.csv

id;Total Due;Due Date
112002209769;3483.28;2015/09/23
142002121343;-197358.33;
600001130260;86578.93;2015/09/22
28002385859;26451.75;2015/09/23
100002232416;983.04;2015/09/23

【讨论】:

  • 谢谢@H4kim 我试过了,你能帮我看看如何将值写入 csvfile 或 txt,我目前正在努力
  • 你是怎么得到这个结果的,我必须添加一个“ print("Total Due", tokens[ind_total_due]) "
  • 通过打印结果列表:print(result) 我编辑我的答案以进行 csv 导出
  • 谢谢@H4kim,这太好了,我目前正在编写这些值,并且还指定了其他要使用多重尝试语句打印出来的值,我的输出看起来像:87,558.93 2015/09/22,它结合了总计和到期日期为一列我希望将其拆分为单独的列,一列用于总计,另一列用于到期日
  • 好的,我会修改我的答案;)
猜你喜欢
  • 2016-10-03
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 1970-01-01
  • 2022-09-23
  • 2020-08-04
相关资源
最近更新 更多