簡體   English   中英

根據前幾行更改文件中的行-Python

[英]Changing line in file based on previous lines - Python

相對較新的python,試圖找出解決此問題的最通用,最易讀的方法。 執行速度也不錯,但這是次要的問題。

我有另一個需要自動編輯的程序的輸入文件。 格式類似於以下內容:

---Thousands of lines that can be ignored---
&Brand: Ford
&Define Class
&Model: Sedan
&Parameter: Cost
&Dollars
&25000
&Parameter: Stock
&Quantity
&14

&Brand: Honda
&Define Class
&Model: Sedan
&Parameter: Cost
&Dollars
&22000
&Parameter: Stock
&Quantity
&17
&Model: SUV
&Parameter: Cost
&Dollars
&35000
&Parameter: Stock
&Quantity
&7
---Thousands of lines that can be ignored---

我的代碼需要自動更改數字參數。 我遇到的麻煩是,我不僅要匹配單個條件並更改線,而且還要匹配非唯一線的唯一組合(參數:“成本”出現了3次,“型號”:“轎車”出現了兩次,“品牌”下出現了兩次。 :本田,但在這兩種情況下都只能使用一次。

正確,我將新參數存儲在嵌套字典中,例如:

params = {'Ford': {'Sedan': {'Cost': 17000, 'Stock': 43}}, 'Honda':{'Sedan': {'Cost': 19000, 'Stock': 12}, {'Truck': {'Cost': 33000, 'Stock': 5}}}

這樣,我可以for brand in params.keys()做,然后for model in params[brand].keys() ,等等。

我具有打開,關閉和修改文件的基礎知識,它可以確定遇到麻煩的要修改的正確行。 謝謝你的幫助。

例如:對於以上示例字典,理想的輸出將是:

---Thousands of lines that can be ignored---
&Brand: Ford
&Define Class
&Model: Sedan
&Parameter: Cost
&Dollars
&17000
&Parameter: Stock
&Quantity
&43

&Brand: Honda
&Define Class
&Model: Sedan
&Parameter: Cost
&Dollars
&19000
&Parameter: Stock
&Quantity
&12
&Model: SUV
&Parameter: Cost
&Dollars
&33000
&Parameter: Stock
&Quantity
&5
---Thousands of lines that can be ignored---

必須查看Python的正則表達式? 看一下“ re”包裝。 您可以使用它來搜索數字條目。 您可以這樣確定感興趣的線(從我的頭頂開始,而不是選中):

import re
...
m = re.match(r'&(\d+)', the_line)
if m:
    print 'found ', m.group(1)
    # modify it...

表達式匹配任意數量的數字(\\ d +部分)。 不知道&是否特殊,但是如果是,可以放在方括號中。

當然,您將需要類似的正則表達式來捕獲之前的行是成本,然后捕獲值。 您可以使用一個簡單的標志來表明網線成本。

參見https://docs.python.org/2/library/re.html

您可以while到達\\n\\n while閱讀行:

import re
model=''
brand=''
whit open('old_file') as f1,open('out_file','w') as f2:

    for line in f1:
          while line !='\n\n':
                if 'Brand' in line :
                    brand=re.match(r'&Brand:(.*)',line).group(1)
                    f2.write(line)
                elif 'Model' in line:
                    model=re.match(r'&Model:(.*)',line).group(1)
                    f2.write(line)

                elif model and brand:
                      if line.strip('&')=='Dollars':
                            f2.write('Dollars'+'\n'+params[brand.strip()][model.strip()]['Cost'])
                      elif line.strip('&')=='Quantity':
                            f2.write('Dollars'+'\n'+params[brand.strip()][model.strip()]['Stock'])

                else:
                      f2.write(line)

這樣的事情可能會起作用。 我創建了一個生成器,然后可以迭代生成更新的文件。

def get_lines(dic):
    brand = ''
    model = ''
    parameter = ''
    with open('testinput.txt', 'r') as fil:
        for line in fil:
            if line[1:].strip().isdigit() and brand in dic and model in dic[brand] and parameter in dic[brand][model]:
                yield '&{0}\n'.format(dic[brand][model][parameter])
            elif line.startswith('&Brand:'):
                brand = line.split(': ')[-1].strip()
                yield line
            elif line.startswith('&Model:'):
                model= line.split(': ')[-1].strip()
                yield line
            elif line.startswith('&Parameter:'):
                parameter= line.split(': ')[-1].strip()
                yield line
            else:
                yield line

params = {'Ford': {'Sedan': {'Cost': 17000, 'Stock': 43}}, 'Honda':{'Sedan': {'Cost': 19000, 'Stock': 12}, 'Truck': {'Cost': 33000, 'Stock': 5}}}

with open('output.txt', 'w') as fil:
    for line in get_lines(params):
        fil.write(line)
import re,fileinput
def print_new_data(brand,model,data):
    print "&Brand: %s"%(brand)
    print "&Define Class"
    print "&Model: %s"%(model)
    print "&Parameter: Cost"
    print "&Dollars"
    print "&%s"%data["cost"]
    print "&Parameter: Stock"
    print "&Quantity"
    print "&%s\n"%data["stock"]        

def process(fh):
    line = next(fh)
    brand= re.findall("Brand: (.*)",line)
    if not brand or brand[0] not in my_list_of_brands::
       print line
       return
    brand = brand[0]
    junk = next(fh)
    model_line = next(fh)
    model_name = re.findall("Model: (.*)",model_line)[0]
    if model_name not in my_data[brand]:
       print line
       print junk
       print model_line
       return
    while line.strip():
       next(fh)
    print_new_data(brand,model,my_data[brand][model])


fh = fileinput.open(["my_data_file"],inplace=1):
while True:
    process(fh)

dang ...我不敢相信我為您浪費了多少時間...

暫無
暫無

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

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