-3

I wrote a small "tool" for my Keylight 34792A, it should take a measurement and output it to me as a variable. It gives me the result in the console(print) and i would like to have the result in a json file, unfortunately I don't know what to do, can someone help me? After each scan, the file should be overwritten from the beginning.

My code:

 #\n after every data
print("Werte:")
for chan in range(1, numberChannels + 1):
    dmm.write("DATA:REMOVE? 1")
    raw = str(dmm.read())      #ergebnisse
    print(raw[0:2] + raw[4] + "." + raw[5:8] + "°" + " " + raw[-4:-1] + " " + zeitpunkt)
Alex
  • 1
  • What you would like the result to look like? Is it a list of the `raw` data? The question isn't about `dmm` so perhaps your example should just be canned `raw` strings. Look at the `json` module in the standard library. – tdelaney Dec 19 '21 at 16:41
  • so, the raw data look like +2.0103403E+1, the string give "Werte: +21.350° 101 2021.12.19-17:32:09 +15.170° 102 2021.12.19-17:32:09 +32.510° 103 2021.12.19-17:32:09 +37.830° 104 2021.12.19-17:32:09 +35.360° 105 2021.12.19-17:32:09" out – Alex Dec 19 '21 at 16:43
  • 1
    Well, do you know how to create JSON data? Do you know how to open and write to a file? Do you know what kind of structure you want the JSON to have? What actually is the *question*? – Karl Knechtel Dec 19 '21 at 16:45
  • Put that info in the question, ideally in a code block where we can copy easily. And what should the output be? Are those space separated fields intended to be part of a list? Include the desired output in the quesiton. – tdelaney Dec 19 '21 at 16:45
  • no i haven't worked with json yet, i think that's the problem. I have to find out more about it. The output should be the same as the print out.. – Alex Dec 19 '21 at 16:48
  • But the print out isn't JSON... they won't be the same. It looks like a list of readings, where each reading is also a list is the most reasonable json format. – tdelaney Dec 19 '21 at 16:52

1 Answers1

0

Use the json module to encode the json to a file. It looks like you have 3 fields so I changed the code to parse them into a list that will be added to the json data.

import json

print("Werte:")
result = []

for chan in range(1, numberChannels + 1):
    dmm.write("DATA:REMOVE? 1")
    raw = str(dmm.read())      #ergebnisse
    data = [raw[0:2] + raw[4] + "." + raw[5:8] + "°", raw[-4:-1], zeitpunkt]
    print(" ".join(data))
    result.append(data)
    
with open("mydata.json", "w") as file:
    json.dump(result, file)
tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • hey thanks for your answer, unfortunately only one line of 5 result in the json.["+14.900\u00b0", "105", "2021.12.19-17:59:06"] – Alex Dec 19 '21 at 17:01
  • Wrote wrong list. Fixed. – tdelaney Dec 19 '21 at 17:02
  • it works, but every result is in the same line, where do I put the "\ n"? – Alex Dec 19 '21 at 17:08
  • The json file? json doesn't add newlines by default. Its written by computers for computers. You can look up "json pretty print" - this question for example https://stackoverflow.com/questions/12943819/how-to-prettyprint-a-json-file?rq=1. – tdelaney Dec 19 '21 at 17:14