0

In file is this:

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

And the output should be like this:

{"RC0805FR -07100 KL": { 
"count": "100", 
"description": "Chip Resistor", 
"value": "100k", 
"unit": "Ohm", ...}

I do not have an idea how to transform the file in the correct format

AMC
  • 2,642
  • 7
  • 13
  • 35
srky
  • 350
  • 2
  • 4
  • 12

2 Answers2

4
with open('myfile.txt') as f:
    s = f.read().strip()
    d = dict([ x.split('=') for x in s.split('\n') ])
    print(d)
Rusty Widebottom
  • 985
  • 2
  • 5
  • 14
1

Hi this should help,

myDict = {}
with open('yourFile.txt', 'r') as myFile: # make sure yourFile.txt is in the same directory as this code, or specify the full path to the text file
    myFile = myFile.readlines()
for item in myFile:
    refined = (item.strip('\n').split('='))
    myDict[refined[0]] = refined[1]
print(myDict)