Cannot write dictionary to file in python using json.dumps() -


i'm getting response json dumps, , i'm trying load in log.txt file, doesn't print out

my function

def get_customer_last_action_executed(self):     """     customer last action executed     inputing customerid     :return:     """     payload ={'customerid': self.customerid}     if not self.customerid:         customer_id = raw_input("please  provide 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     open('log.txt', 'w') f:         json.dumps(payload, f) 

what need use dump() not dumps().

json.dump()

serialize obj json formatted stream fp (a .write()-supporting file-like object

if ensure_ascii false, chunks written fp may unicode instances

json.dumps()

serialize obj json formatted str

if ensure_ascii false, result may contain non-ascii characters , return value may unicode instance

full details can found on this thread


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -