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)
1
  • Edit your Question and show def self.send_request(.... Commented Jul 25, 2017 at 18:12

1 Answer 1

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

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

2 Comments

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))
As mentioned in the answer, you need to use dump not dumps. Remove the "s" :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.