将字典数据附加到 python 中的列表时输出发生变化
output changes while appending dictionary data into list in python
我有一本包含特定格式数据的字典。当我单独打印字典时,它会正确打印数据,但是当我将所有字典添加到列表中然后打印列表时,它会打印错误的输出。我可以知道这个问题的解决方案吗?
我的代码:
import glob
import os
list_for_dictionary = []
list_for_dictionary_2 = []
data_location = 'D:\sample_data' # folder containing all the data
for root, directories, files in os.walk(data_location):
for directory in directories:
loc = (data_location + '/' + directory + '/*')
all_files = glob.glob(loc)
for filename in all_files:
list_for_dictionary.clear()
f = open(filename)
for line_number, line in enumerate(f, 1):
dictionary = {"fileLine": line.strip(), "lineNumber": int(line_number), "fileName": filename.split("\")[-1]}
list_for_dictionary.append(dictionary)
dictionary_2={"collection":[], "documentName":filename.split("\")[-1],"returnId":0,"docData":{"Matches":list_for_dictionary}}
print(dictionary_2)
list_for_dictionary_2.append(dictionary_2)
print(list_for_dictionary_2)
预期输出:
[{'collection': [], 'documentName': 'sample.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: John', 'lineNumber': 1, 'fileName': 'sample.txt'}, {'fileLine': 'Age: 24 years', 'lineNumber': 2, 'fileName': 'sample.txt'}]}}
{'collection': [], 'documentName': 'sample2.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Matthews', 'lineNumber': 1, 'fileName': 'sample2.txt'}, {'fileLine': 'Age: 32 years', 'lineNumber': 2, 'fileName': 'sample2.txt'}]}}
{'collection': [], 'documentName': 'sample3.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Alisa', 'lineNumber': 1, 'fileName': 'sample3.txt'}, {'fileLine': 'Age: 40 years', 'lineNumber': 2, 'fileName': 'sample3.txt'}]}}
{'collection': [], 'documentName': 'sample4.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Jacob', 'lineNumber': 1, 'fileName': 'sample4.txt'}, {'fileLine': 'Age: 60 years', 'lineNumber': 2, 'fileName': 'sample4.txt'}]}}]
我的输出:
[{'collection': [], 'documentName': 'sample.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Jacob', 'lineNumber': 1, 'fileName': 'sample4.txt'}, {'fileLine': 'Age: 60 years', 'lineNumber': 2, 'fileName': 'sample4.txt'}]}},
{'collection': [], 'documentName': 'sample2.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Jacob', 'lineNumber': 1, 'fileName': 'sample4.txt'}, {'fileLine': 'Age: 60 years', 'lineNumber': 2, 'fileName': 'sample4.txt'}]}},
{'collection': [], 'documentName': 'sample3.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Jacob', 'lineNumber': 1, 'fileName': 'sample4.txt'}, {'fileLine': 'Age: 60 years', 'lineNumber': 2, 'fileName': 'sample4.txt'}]}},
{'collection': [], 'documentName': 'sample4.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Jacob', 'lineNumber': 1, 'fileName': 'sample4.txt'}, {'fileLine': 'Age: 60 years', 'lineNumber': 2, 'fileName': 'sample4.txt'}]}}]
看起来像是引用错误。您正在执行 "docData":"Matches":list_for_dictionary}}
,然后在后续循环中覆盖 list_for_dictionary
。
初始化一个新的 list_for_dictionary = []
,而不是清除 list_for_dictionary.clear()
import glob
import os
list_for_dictionary = []
list_for_dictionary_2 = []
data_location = 'D:\sample_data' # folder containing all the data
for root, directories, files in os.walk(data_location):
for directory in directories:
loc = (data_location + '/' + directory + '/*')
all_files = glob.glob(loc)
for filename in all_files:
list_for_dictionary = []
f = open(filename)
for line_number, line in enumerate(f, 1):
dictionary = {"fileLine": line.strip(), "lineNumber": int(line_number), "fileName": filename.split("\")[-1]}
list_for_dictionary.append(dictionary)
dictionary_2={"collection":[], "documentName":filename.split("\")[-1],"returnId":0,"docData":{"Matches":list_for_dictionary}}
print(dictionary_2)
list_for_dictionary_2.append(dictionary_2)
print(list_for_dictionary_2)
我有一本包含特定格式数据的字典。当我单独打印字典时,它会正确打印数据,但是当我将所有字典添加到列表中然后打印列表时,它会打印错误的输出。我可以知道这个问题的解决方案吗?
我的代码:
import glob
import os
list_for_dictionary = []
list_for_dictionary_2 = []
data_location = 'D:\sample_data' # folder containing all the data
for root, directories, files in os.walk(data_location):
for directory in directories:
loc = (data_location + '/' + directory + '/*')
all_files = glob.glob(loc)
for filename in all_files:
list_for_dictionary.clear()
f = open(filename)
for line_number, line in enumerate(f, 1):
dictionary = {"fileLine": line.strip(), "lineNumber": int(line_number), "fileName": filename.split("\")[-1]}
list_for_dictionary.append(dictionary)
dictionary_2={"collection":[], "documentName":filename.split("\")[-1],"returnId":0,"docData":{"Matches":list_for_dictionary}}
print(dictionary_2)
list_for_dictionary_2.append(dictionary_2)
print(list_for_dictionary_2)
预期输出:
[{'collection': [], 'documentName': 'sample.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: John', 'lineNumber': 1, 'fileName': 'sample.txt'}, {'fileLine': 'Age: 24 years', 'lineNumber': 2, 'fileName': 'sample.txt'}]}}
{'collection': [], 'documentName': 'sample2.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Matthews', 'lineNumber': 1, 'fileName': 'sample2.txt'}, {'fileLine': 'Age: 32 years', 'lineNumber': 2, 'fileName': 'sample2.txt'}]}}
{'collection': [], 'documentName': 'sample3.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Alisa', 'lineNumber': 1, 'fileName': 'sample3.txt'}, {'fileLine': 'Age: 40 years', 'lineNumber': 2, 'fileName': 'sample3.txt'}]}}
{'collection': [], 'documentName': 'sample4.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Jacob', 'lineNumber': 1, 'fileName': 'sample4.txt'}, {'fileLine': 'Age: 60 years', 'lineNumber': 2, 'fileName': 'sample4.txt'}]}}]
我的输出:
[{'collection': [], 'documentName': 'sample.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Jacob', 'lineNumber': 1, 'fileName': 'sample4.txt'}, {'fileLine': 'Age: 60 years', 'lineNumber': 2, 'fileName': 'sample4.txt'}]}},
{'collection': [], 'documentName': 'sample2.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Jacob', 'lineNumber': 1, 'fileName': 'sample4.txt'}, {'fileLine': 'Age: 60 years', 'lineNumber': 2, 'fileName': 'sample4.txt'}]}},
{'collection': [], 'documentName': 'sample3.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Jacob', 'lineNumber': 1, 'fileName': 'sample4.txt'}, {'fileLine': 'Age: 60 years', 'lineNumber': 2, 'fileName': 'sample4.txt'}]}},
{'collection': [], 'documentName': 'sample4.txt', 'returnId': 0, 'docData': {'Matches': [{'fileLine': 'Name of Person: Jacob', 'lineNumber': 1, 'fileName': 'sample4.txt'}, {'fileLine': 'Age: 60 years', 'lineNumber': 2, 'fileName': 'sample4.txt'}]}}]
看起来像是引用错误。您正在执行 "docData":"Matches":list_for_dictionary}}
,然后在后续循环中覆盖 list_for_dictionary
。
初始化一个新的 list_for_dictionary = []
,而不是清除 list_for_dictionary.clear()
import glob
import os
list_for_dictionary = []
list_for_dictionary_2 = []
data_location = 'D:\sample_data' # folder containing all the data
for root, directories, files in os.walk(data_location):
for directory in directories:
loc = (data_location + '/' + directory + '/*')
all_files = glob.glob(loc)
for filename in all_files:
list_for_dictionary = []
f = open(filename)
for line_number, line in enumerate(f, 1):
dictionary = {"fileLine": line.strip(), "lineNumber": int(line_number), "fileName": filename.split("\")[-1]}
list_for_dictionary.append(dictionary)
dictionary_2={"collection":[], "documentName":filename.split("\")[-1],"returnId":0,"docData":{"Matches":list_for_dictionary}}
print(dictionary_2)
list_for_dictionary_2.append(dictionary_2)
print(list_for_dictionary_2)