需要帮助编写Python程序来替换文本文件中的占位符单词[已关闭]

问题描述 投票:0回答:1

我正在编写一个程序,用字典中的单词(值)替换文件中的某些单词(在[]中)。问题是输出重复一行多次

输入文件(Template_Mail.txt)

Introducing [Product] - Unmatched Quality at an Irresistible Price!

Dear [Customer_Name],

[Company] is thrilled to present our latest innovation, the [Product]—a testament to our commitment to delivering excellence at an unbeatable value.

Key Features:

[Feature1]: Experience [Product] in all its glory with [Feature1_Detail].

输入文件公司1

Customer_Name-Rohit Arya
Company-DemoIndustry
Product-Calling Card
Feature1-Low Internationl Rates
Feature1_Detail-Lowest International Rates in the Country

代码

import re
with open('Template_Mail.txt', 'r') as file: #opening and reading the text file
    lines = file.readlines() #realines() will store text in 'lines'
    file.close()
company1={} #creating empty dictionary to store details
#creating a dictionary for company 1
with open('company1.txt', 'r') as f: 
    # Read the contents of the file into a list 
    lines2 = f.readlines()
    # Loop through the list of lines 
    for line in lines2: 
        # Split the line into key-value pairs 
        key, value = line.strip().split('-') 
        # Store the key-value pairs in the dictionary 
        company1[key] = value

Text=''
for i in range(len(lines)):  #loop to read each element (line) of the list
  if(lines[i].find("[Product]")): #finds if [Product] is present in the line
    Text +=lines[i].replace("[Product]",company1['Product'] )
    #replaces [Product] by the company1 details
  if(lines[i].find("[Customer_Name]")):
    Text +=lines[i].replace("[Customer_Name]",company1['Customer_Name'] )
  if(lines[i].find("[Company]")):
    Text +=lines[i].replace("[Company]",company1['Company'] )
  if(lines[i].find("[Feature1]")):
    Text +=lines[i].replace("[Feature1]",company1['Feature1'] )
  if(lines[i].find("[Feature1_Detail]")):
    Text +=lines[i].replace("[Feature1_Detail]",company1['Feature1_Detail'] )
  if(lines[i].find("[Price]")):
    Text +=lines[i].replace("[Price]",company1['Price'] )
  if(lines[i].find("[Support_Mail]")):
    Text +=lines[i].replace("[Support_Mail]",company1['Support_Mail'] )

print(Text)

输出

Introducing Calling Card - Unmatched Quality at an Irresistible Price!
Introducing [Product] - Unmatched Quality at an Irresistible Price!
Introducing [Product] - Unmatched Quality at an Irresistible Price!
Introducing [Product] - Unmatched Quality at an Irresistible Price!
Introducing [Product] - Unmatched Quality at an Irresistible Price!
Introducing [Product] - Unmatched Quality at an Irresistible Price!

Dear [Customer_Name],
Dear Rohit Arya,
Dear [Customer_Name],
Dear [Customer_Name],
Dear [Customer_Name],
Dear [Customer_Name],


[Company] is thrilled to present our latest innovation, the Calling Card—a testament to our commitment to delivering excellence at an unbeatable value.
[Company] is thrilled to present our latest innovation, the [Product]—a testament to our commitment to delivering excellence at an unbeatable value.
[Company] is thrilled to present our latest innovation, the [Product]—a testament to our commitment to delivering excellence at an unbeatable value.
[Company] is thrilled to present our latest innovation, the [Product]—a testament to our commitment to delivering excellence at an unbeatable value.
[Company] is thrilled to present our latest innovation, the [Product]—a testament to our commitment to delivering excellence at an unbeatable value.
[Company] is thrilled to present our latest innovation, the [Product]—a testament to our commitment to delivering excellence at an unbeatable value.

我期望输出如下:


Introducing Calling Card - Unmatched Quality at an Irresistible Price!

Dear Rohit Arya,

DemoIndustry is thrilled to present our latest innovation, the Calling Card—a testament to our commitment to delivering excellence at an unbeatable value.
python regex python-2.7 templates text-files
1个回答
0
投票

也许模板的格式是在本练习中为您设置的。但如果不是,则只需将模板中的所有 [] 对替换为 {} 和调用格式即可。所以类似:

from io import StringIO

template_mail_txt = """Introducing {Product} - Unmatched Quality at an Irresistible Price!

Dear {Customer_Name},

{Company} is thrilled to present our latest innovation, the {Product}—a testament to our commitment to delivering excellence at an unbeatable value.

Key Features:

{Feature1}: Experience {Product} in all its glory with {Feature1_Detail}."""

company_1_txt = """Customer_Name-Rohit Arya
Company-DemoIndustry
Product-Calling Card
Feature1-Low Internationl Rates
Feature1_Detail-Lowest International Rates in the Country"""


company1={} #creating empty dictionary to store details
#creating a dictionary for company 1
#with open('company1.txt', 'r') as f:
with StringIO(company_1_txt) as f: 
    # Read the contents of the file into a list 
    lines2 = f.readlines()
    # Loop through the list of lines 
    for line in lines2: 
        # Split the line into key-value pairs 
        key, value = line.strip().split('-') 
        # Store the key-value pairs in the dictionary 
        company1[key] = value
        
#with open('Template_Mail.txt', 'r') as file: #opening and reading the text file
with StringIO(template_mail_txt) as file:
    lines = file.read() # You don't need this as individual lines
    # file.close() <- Don't need with 'with'
    text = lines.format(**company1)
© www.soinside.com 2019 - 2024. All rights reserved.