簡體   English   中英

如何跳過2個文件中的大量文本或值並使用數據執行其他任務

[英]How to skip a lot of text or values in 2 files and do another task with the data

在下面的代碼中,我想跳過內容(文件ex1.idlex2.idl很多不可用的內容),並獲取要使用的數據。 此數據從每個文件的每一行的第905個值開始。 這些文件的片段為:

ex1.idl

0.11158E-13 0.11195E-13 0.11233E-13 ...

ex2.idl

0.11010E-13 0.11070E-13 0.11117E-13 ...

我可以成功跳過不需要的值。 我還可以進行一些拆分,切片和計算。 但是當我將兩者結合時,代碼似乎無法正常工作。 以下是我擁有的組合代碼:

with open('ex1.idl') as f1, open('ex2.idl') as f2:
    with open('ex3.txt', 'w') as f3:

        a = 905                           #the first part
        f1 = f1.readlines(905:)[a-1:]     #the first part
        f2 = f2.readlines(905:)[a-1:]     #the first part

        f1 = map(float, f1.read().strip().split())              #the second part
        f2 = map(float, f2.read().strip().split())              #the second part
        for result in map(lambda v: v[0]/v[1], zip(f1, f2)):    #the second part
            f3.write(str(result)+"\n")                          #the second part

這是我僅讀取數據並單獨進行拆分和計算的代碼。 這有效:

with open('primer1.idl') as f1, open('primer2.idl') as f2:
with open('primer3.txt', 'w') as f3:

    f1 = map(float, f1.read().strip().split())
    f2 = map(float, f2.read().strip().split())
    for result in map(lambda v: v[0]/v[1], zip(f1, f2)):

        f3.write(str(result)+"\n")

因此,我只想補充一點,該程序在905行開始讀取和計算。

預先感謝您的回答。

我在這里做了一些工作,發現這也行得通:

with open('ex1.idl') as f1, open('ex2.idl') as f2:
    with open('ex3.txt', 'w') as f3:

        start_line = 905        #reading from this line forward
        for i in range(start_line - 1):
            next(f1)
            next(f2)
        f1 = list(map(float, f1.read().split()))
        f2 = list(map(float, f2.read().split()))
        for result in map(lambda v : v[0]/v[1], zip(f1,f2)):
            f3.write((str(result)) + '\n')

嘗試這個:

from itertools import islice


with open('ex1.idl') as f1, open('ex2.idl') as f2:
    with open('ex3.txt', 'w') as f3:
        f1 = islice(f1, 905, None)  # skip first 905 lines
        f2 = islice(f2, 905, None)  # skip first 905 lines

        for f1_line, f2_line in zip(f1, f2):
            f1_vals = map(float, f1_line.strip().split())
            f2_vals = map(float, f2_line.strip().split())
            for result in map(lambda v: v[0]/v[1], zip(f1_vals, f2_vals)):
                f3.write(str(result)+"\n")

這將忽略每個文件中的前904個值。 然后,它將文件的內容壓縮在一起,以便將每個文件中的相應行放到同一元組中。 然后,您可以遍歷此壓縮數據並拆分空間上的每一行,將值轉換為浮點值,然后進行除法。

請注意,如果兩個文件中的任何一行都包含零,那么您很可能會得到一個float division by zero為單位的float division by zero 請確保文件中沒有零。 如果無法確保其中沒有零,則應使用try-except處理,然后跳過:

with open('ex1.idl') as f1, open('ex2.idl') as f2:
    with open('ex3.txt', 'w') as f3:
        f1 = islice(f1, 905, None)  # skip first 905 lines
        f2 = islice(f2, 905, None)  # skip first 905 lines

        for f1_line, f2_line in zip(f1, f2):
            f1_vals = map(float, f1_line.strip().split())
            f2_vals = map(float, f2_line.strip().split())
            for v1, v2 in zip(f1_vals, f2_vals):
                try:
                    result = v1/v2
                    f3.write(str(result)+"\n")
                except ZeroDivisionError:
                    print("Encountered a value equal to zero in the second file. Skipping...")
                    continue

除了跳過之外,您還可以執行其他任何您想做的事情。 隨你(由你決定。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM