第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

根據(jù)第一個(gè)值列出索引超出范圍python2.7循環(huán)

根據(jù)第一個(gè)值列出索引超出范圍python2.7循環(huán)

桃花長(zhǎng)相依 2022-11-18 16:49:30
美好的一天可能有人請(qǐng)幫助我嘗試基于第一個(gè)值“tocken”循環(huán)遍歷 txt 文件,它當(dāng)前僅在我指定 tockens 時(shí)循環(huán),例如 {600001130260} 文件中的多個(gè) tockens 都在第一行 [0 ] id 喜歡它迭代每個(gè)標(biāo)記/行并提取指定的信息。數(shù)據(jù)文件看起來(lái)像這樣600001130260|005|||IN|2197|01||20160905210028600001130260|100|005|00|VAT|VAT|VAT @ 14%|2,150.14600001130260|100|013|00|TOT|CTOT|Total Due|86,578.93600001130260|100|014|00|DD|DD|Due Date|2015/09/22|2015/10/15600001130260|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.4828002385859|100|006|00|VAT|VAT|VAT @ 14%|10.6228002385859|100|013|00|TOT|CTOT|Total Due|26,451.7528002385859|100|014|00|DD|DD|Due Date|2015/09/2328002385859|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')我希望它在不指定 [0] 中的標(biāo)記的情況下給我輸出 total_due
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

你想要的所有值Total Due嗎?如果是這樣,您只需執(zhí)行以下操作:


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


結(jié)果將是:


[('Total Due', '86,578.93'),

 ('Total Due', '26,451.75'),

 ('Total Due', '3,483.28'),

 ('Total Due', '983.04'),

 ('Total Due', '- 197,358.33')]

第一個(gè)“令牌”似乎是一個(gè)唯一標(biāo)識(shí)符。如果您想要 csv 導(dǎo)出和多列支持,您可以這樣做:


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


查看完整回答
反對(duì) 回復(fù) 2022-11-18
  • 1 回答
  • 0 關(guān)注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)