.append在嵌套if中没有按顺序添加



我正试图找到特定的值,编辑它们并将它们写回列表。如果值不在列表中,则按原样追加。

columns_list = ["Section", "String ENG", "AZ", "RU"]
loco_df = pd.read_csv("loco_auto_in.csv", usecols=columns_list)

main_file = open('bayg_t.txt','r')
eng_txt_list = []
az_txt_list = [] 
for line in main_file:
eng_txt_list.append(line)
for i in loco_df["String ENG"]:
for x in eng_txt_list:
tr_strings = re.findall(r'msgid+."(.*?)"', x)
if tr_strings:
if i == tr_strings[0]: 
e_x = x.replace(tr_strings[0], "00000000000xxxxx0000")
az_txt_list.append(e_x)
elif x not in az_txt_list: 
az_txt_list.append(x)

elif x not in az_txt_list: 
az_txt_list.append(x)

print(az_txt_list)

问题是:它总是添加编辑"00000000000xxxxx000 "然而,在列表的末尾,在列表的中间有匹配项。我已经检查过了,但看不出原因。

提前谢谢你。

输出:

['#: views/pages/announcements.php:59, templates/single/course/instructors.php:72n', 'msgid "Courses"n', 'msgstr ""n', 'n', '#: templates/student-public-profile.php:121n', 'msgid "Courses Completed"n', '#: templates/student-public-profile.php:116n', 'msgid "Courses Enrolled"n', '#: classes/Options.php:272, classes/Tutor_Setup.php:412n', 'msgid "Courses Per Page"n', 'msgid "00000000000xxxxx0000"n', 'msgid "00000000000xxxxx0000"n', 'msgid "00000000000xxxxx0000"n', 'msgid "00000000000xxxxx0000"n', 'msgid "00000000000xxxxx0000"n', 'msgid "00000000000xxxxx0000"n', 'msgid "00000000000xxxxx0000"n', 'msgid "00000000000xxxxx0000"n', 'msgid "00000000000xxxxx0000"n', 'msgid "00000000000xxxxx0000"n', 'msgid "00000000000xxxxx0000"n']

问题嵌套为。

for i in loco_df["String ENG"]:
string_list.append(i)

for x in eng_txt_list:
tr_strings = re.findall(r'msgid+."(.*?)"', x)
if tr_strings:
if tr_strings[0] in string_list: 
print("Matched!")
e_x = x.replace(tr_strings[0], "00000000000xxxxx0000")
az_txt_list.append(e_x)
elif x not in az_txt_list: 
az_txt_list.append(x)

elif x not in az_txt_list: 
az_txt_list.append(x)
print(az_txt_list)

输出:['#: templates/dashboard/assignments.php:34,n', '#: templates/dashboard/purchase_history.php:21,n', '#: templates/shortcode/tutor-instructor.php:27,n', '#: views/pages/announcements.php:59, templates/single/course/instructors.php:72n', 'msgid "00000000000xxxxx0000"n', 'msgstr ""n', 'n', '#: templates/student-public-profile.php:121n', 'msgid "00000000000xxxxx0000"n', '#: templates/student-public-profile.php:116n', 'msgid "00000000000xxxxx0000"n', '#: classes/Options.php:272, classes/Tutor_Setup.php:412n', 'msgid "00000000000xxxxx0000"n', '#: classes/Tutor_Setup.php:407n', 'msgid "00000000000xxxxx0000"n', '#: classes/Utils.php:4770n', 'msgid "00000000000xxxxx0000"n', '#: templates/dashboard/settings/profile.php:50n', 'msgid "00000000000xxxxx0000"n']

最新更新