简体   繁体   English

替换列表中出现的字符串部分

[英]Replace parts of string that occur within a list

Alright I'm making a program that needs to take information about a customer from a user, there are various fields that need to be filled in. And if the values of the alphanumeric values are left blank I need to by default set that value to blank, so a single whitesapce character. 好的,我正在编写一个程序,该程序需要从用户那里获取有关客户的信息,需要填写各个字段。而且,如果字母数字值的值留为空白,则我默认需要将该值设置为空白,因此是一个空白字符。 And I need to make it equal to 0 if the numeric values are left blank 如果数值留空,我需要使其等于0

    def insertion(new_record):
    field_names = ['Customer_ID=',
    'Last_Name=',
    'First_Name=',
    'Street_Address=',
    'City=',
    'Province=',
    'Postal_Code=',
    ]
    field_names2 = ['Home_Phone=',
    'Business_Phone=']

    if new_record[3:5] == 'CU':
        for i in field_names:
            if i in new_record:
                new_record.replace()

The idea here is that I have list of the what the field names would look like if there were left blank by the user. 这里的想法是,我列出了如果用户将其保留为空白,则字段名称将是什么样的列表。 So if any of those items in field_names or numeric_field_names appear in the string then it means the user left that field blank. 因此,如果field_namesnumeric_field_names中的任何一项出现在字符串中,则意味着用户将该字段留为空白。 And I want to replace those strings in the input accordingly, then write them to a file. 我想相应地替换输入中的那些字符串,然后将它们写入文件。 Can I iterate through new_record in this case to replace those strings? 我可以在这种情况下遍历new_record来替换那些字符串吗? Note also that the input will be multiple lines always. 还要注意,输入将始终是多行。

EDIT 编辑

This is how I call the function: 这就是我所谓的函​​数:

insertion('''IN CU
Customer_ID=474
Last_Name=Sanderson
First_Name=John
Street_Address=17 Chestwood Ave
City=Scarborough
Province=Ont
Postal_Code=M9C2C7
Home_Phone=416/227-3297
Business_Phone=416/997-2923
//'''
)

Instead of using list, you can use dictionary in the following way. 除了使用列表,您还可以通过以下方式使用字典。

def insertion(new_record):
    newRecordDict={r.split('=')[0]:r.split('=')[1] for r in new_record.split('\n')[1:-1]} #first and last line is ignored
    field_names = {'Customer_ID':None,
    'Last_Name':None,
    'First_Name':None,
    'Street_Address':None,
    'City':None,
    'Province':None,
    'Postal_Code':None,
    }
    field_names2 = {'Home_Phone':0,'Business_Phone':0}

    for field in field_names:
        field_names[field] = newRecordDict.get(field,None)
    for field in field_names2:
        field_names2[field] = newRecordDict.get(field,None)

    print field_names
    print field_names2

Actually, you can keep all data in to one dictioanary instead of field_names and field_names2.You can take a call for the same. 实际上,您可以将所有数据保留在一个字典中,而不是field_names和field_names2。您可以致电查询。 In the above code, all data coming from the user loaded as dictionary in newRecordDict after that based on updated field, field_names and field_names2 gets updated. 在上面的代码中,所有来自作为字典的用户的数据都将在newRecordDict中基于更新的字段field_names和field_names2进行更新。 Following is the output: 以下是输出:

{'Province': 'Ont', 'City': 'Scarborough', 'First_Name': 'John', 'Last_Name': 'Sanderson', 'Postal_Code': 'M9C2C7', 'Customer_ID': '474', 'Street_Address': '17 Chestwood Ave'} 

{'Business_Phone': '416/997-2923', 'Home_Phone': '416/227-3297'}

Now, if you want to access 'Province' from field_names, you can use: 现在,如果要从field_names访问“省”,则可以使用:

field_names['Province']

which returns 'Ont'. 返回“ Ont”。 Similarly it works for all other fields. 同样,它适用于所有其他领域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM