[英]Python - dictionary iteration and replacement fails on same case each time
我遍歷字典。 鍵是我想用“新值”替換的“舊值”,即字典中的值。
我能夠在大多數情況下進行替換。 但是,我總是發現字典的第二個條目(“02-more text”)總是在其他經過清理的輸出文件中。
我究竟做錯了什么? 我讀過 Python 不喜歡替換它正在迭代的列表。 所以,我有一個“for 循環”附加到的新列表。 其中我有一個“臨時行”,它復制原始“csv_rows”中的行。
為什么“02-more text”總是在輸出文件中?
原始文件是一個 CSV 文件。 轉動數據框“tolist”使數據框的每一行成為更大列表“csv_rows”中的一個列表。
import pandas as pd
import csv
from csv import writer
dictionary = {
"01-some text" : "replacement",
"02-more text" : "replacement",
"03-even more text" : "replacement",
"01-text" : "replacement",
"02-another lorem" : "replacement",
"03-ipsum" : "replacement",
"04-dolorem" : "replacement"
}
def append_list_as_row(file_name, list_of_elem):
# Open file in append mode
with open(file_name, 'a+', newline='', encoding='utf-8') as write_obj:
# Create a writer object from csv module
csv_writer = writer(write_obj)
# Add contents of list as last row in the csv file
csv_writer.writerow(list_of_elem)
def get_file_encoding(src_file_path):
"""
Get the encoding type of a file
:param src_file_path: file path
:return: str - file encoding type
"""
with open(src_file_path) as src_file:
return src_file.encoding
data = 'ANQAR.csv'
my_encoding = str(get_file_encoding(data))
df = pd.read_csv(data, encoding=my_encoding)
csv_rows = df.values.tolist()
new_list = []
for key in dictionary:
for row in csv_rows:
temp_row = row
if key in row:
#find the index
i = row.index(key)
#replace value with new one
temp_row[i] = dictionary[key]
new_list.append(temp_row)
for row in new_list:
append_list_as_row('newANQAR.csv', row)
您沒有正確進行消毒。
嘗試運行以下代碼段,看看它是否適合您:
replacements_map = {
"01-some text": "replacement",
"02-more text": "replacement",
"03-even more text": "replacement",
"01-text": "replacement",
"02-another lorem": "replacement",
"03-ipsum": "replacement",
"04-dolorem": "replacement"
}
csv_rows = [["01-some text", " other data here"],
["wont need replacement", "02-another lorem"]]
sanitized_rows = []
for row in csv_rows:
sanitized_rows.append(
[(replacements_map[item] if (item in replacements_map) else item)
for item
in row]
)
print(sanitized_rows)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.