1

I'm getting a response from a JSON dumps, and I'm trying to load it in a log.txt file, but this doesn't print out anything

My Function

def get_customer_last_action_executed(self):
    """
    Get the customer last action executed
    By inputing the CustomerID
    :return:
    """
    payload ={'customerID': self.CustomerID}
    if not self.CustomerID:
        customer_id = raw_input("Please  provide the customer ID:")
        self.CustomerID = customer_id
        # raise Exception('No customerID provided')

    response = self.send_request(self.get_customer_last_action_executed_url + self.CustomerID,
                                 json.dumps(payload),
                                 "GET")

    print response.url, response.status_code
    print response, response.text, response.reason
    if response:
        print self.sucessful_msg
    else:
        print self.error_msg
    with open('log.txt', 'w') as f:
        json.dumps(payload, f)
Nabin
  • 11,216
  • 8
  • 63
  • 98
Patcho
  • 77
  • 8

1 Answers1

2

What you need to use is dump() not dumps().

json.dump()

Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object

If ensure_ascii is False, some chunks written to fp may be unicode instances

json.dumps()

Serialize obj to a JSON formatted str

If ensure_ascii is False, the result may contain non-ASCII characters and the return value may be a unicode instance

Full details can be found on this thread

Nabin
  • 11,216
  • 8
  • 63
  • 98
  • Hi it doesnt still work , i've used this , with io.open('log.txt', 'r+', encoding='utf-8') as f: # f.write(json.dumps(payload, ensure_ascii=False)) – Patcho Jul 25 '17 at 12:29
  • As mentioned in the answer, you need to use dump not dump*s*. Remove the "s" :D – Nabin Jul 25 '17 at 15:13