简体   繁体   English

Save.txt 文件以特定格式保存在字典中

[英]Save .txt file in a dict with specific format

I have a problem by reading my.txt file and save it in dict.我通过阅读 my.txt 文件并将其保存在 dict 中遇到问题。 This is my following code:这是我的以下代码:

 def __init__(self, folder_path):
    os.chdir(folder_path)
    self._path = folder_path
    self._data = {}
    _files = glob.glob('*.txt')
    _temp = {}
    for dat in _files:
        _temp.clear()
        with open(dat,"r",encoding="utf-8") as f:
            for item in f:
                if item != '\n':
                    custom = (item.strip('\n').split('='))
                    _temp[custom[0]] = custom[1]
                    self._data[dat] = _temp
    print(self._data)

And this is the output:这是 output:

{'RC0603FR-07100KL.txt': {'count': '20', 'value': 'TPS73033', 'package': 'SOT-23-5'}, 
'RC0805FR-07100KL.txt': {'count': '20', 'value': 'TPS73033', 'package': 'SOT-23-5'}, 
'TPS73033DBVT.txt': {'count': '20', 'value': 'TPS73033', 'package': 'SOT-23-5'}}

The exactly problem is, that the last value override the all other one.确切的问题是,最后一个值会覆盖所有其他值。 For ex.例如。 this is how it should look like:这应该是这样的:

{'RC0603FR-07100KL.txt': {'count': '100', 'value': '100k', 'package': 'Chip'}, 
'RC0805FR-07100KL.txt': {'count': '50', 'value': '10n', 'package': 'Cap'}, 
'TPS73033DBVT.txt': {'count': '20', 'value': 'TPS73033', 'package': 'SOT-23-5'}}

What I'm doing wrong?我做错了什么?

A sample of.txt file: .txt 文件示例:

count=50
value=100k
unit=Ohm
package=0603
description=Chip Resistor
supplier=Digikey
supplierpartnumber=311-100KHRCT-ND
price=0.009
currency=CHF

You need to copy the dict.你需要复制字典。 Replace self._data[dat] = _temp by self._data[dat] = _temp.copy()self._data[dat] = _temp替换为self._data[dat] = _temp.copy()

Check this example:检查这个例子:

data = [
    {'a':1, 'b':1},
    {'a':2, 'b':2},
]
copieddata = []
temp = {}
for d in data:
    temp.clear()
    for k, v in d.items():
        temp[k] = v
    copieddata.append(temp)

This will have copieddata as:这将copieddata为:

[{'a': 2, 'b': 2}, {'a': 2, 'b': 2}]

Because, you are basically using the same variable temp and assign it to different elements of the list ( copieddata ).因为,您基本上使用相同的变量temp并将其分配给列表的不同元素( copieddata )。 But what you want is that you want to create different dict and assign it to elements of the list, hence you need to copy the temp variable.但是您想要的是创建不同的dict并将其分配给列表的元素,因此您需要复制temp变量。 That ensures new dict is created and not the same one is reused for all the elements of the list ( copieddata ).这样可以确保创建新的 dict,并且不会为列表的所有元素( copieddata )重用同一个。 Below I used .copy() to add to the list, and works as expected.下面我使用.copy()添加到列表中,并按预期工作。

data = [
    {'a':1, 'b':1},
    {'a':2, 'b':2},
]
copieddata = []
temp = {}
for d in data:
    temp.clear()
    for k, v in d.items():
        temp[k] = v
    copieddata.append(temp.copy())

Copied data is now:复制的数据现在是:

[{'a': 1, 'b': 1}, {'a': 2, 'b': 2}]

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

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