-2

I am new to python and researched a log (probably the wrong way) and now decided to ask:

I have a bash script, which outputs two values. In a python script I invoke this this script by

subprocess.Popen(...skript details...)
(output, err) = p.communicate()
p_status = p.wait()
print "Command output : ", output
print "Command exit status/return code : ", p_status
value1, value2 = output.split(' ',1)

If I now add

print value1

I get what I expect (a numeric value).

Now I got some code from github to post data within a python script using curl. This builds the body like

body='{"mode":"async", "messageType":"1", "messages":[{"Name of Value1":value1, "Name of Value2":value2}]}'

However, what gets posted is the text "value1" and "value2" and not the actual values of the variables. I tried a lot of masking here but could not get any valuable result. Thx in advance

2
  • 1
    "temp" and "humid"? No variables or values by those names appear in any of the code you posted. Please clarify your question. Commented Dec 4, 2015 at 22:02
  • check, these are actually values 1 and 2... corrected Commented Dec 4, 2015 at 22:09

1 Answer 1

2

If I understood you correct, you need string formatting.

body='{"mode":"async", "messageType":"1", "messages":[{"Name of Value1":{0}]}'.format(value1)

It is done by built in format function.

Here is what you need to do for your example:

>>> humidity = 99

>>> temp = 100

>>> print "[ {{ temperature: {0}, humidity: {1} }} ]".format(temp, humidity)

[ { temperature: 100, humidity: 99 } ]

Note: to escape {} from formatting use double braces {{}} around.

Sign up to request clarification or add additional context in comments.

11 Comments

Thx, I think that's not exactly, what I need. Putting it different: value 1 is humidity, e.g. 57.5 . I need something like [{"Humidity":57.5}]
what do you have inside value1 variable, and what do you want to have inside your body string?
Value1 is called Humidity and contains a numeric value like 57.5. From my body string I get the following posted: {"Humidity": humid} (first is correct, but second is wrong, should be the numeric contents of the variable).
what is printed from print value1?
57.5. Let's look at the original code without alienation: I have defined variables with: humid, temp = output.split(' ',1) and print shows correct values. Body end part is "messages":[{"temperature":temp, "humidity":humid}]}' and outputs temperature:temp,humidity:humid instead of desired temperature:75.4,humid:57.5
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.