I want to write to the file structure:

class MyData(object): def __init__(self): self.m_nID = 0 self.m_strTitle = "" def serialize(self): temp_dict = { 'ID': self.m_nID, 'Title': self.m_strTitle} return temp_dict def setData2File(a_data, a_str_path): file = open(a_str_path, "w") json.dump(a_data, file, ensure_ascii=False, indent=4) if __name__ == '__main__': # ... work ... work ... work for result in arrResults: FileUtils.setData2File(result.serialize() , "test.json") 

Everything is perfectly recorded in the file, BUT: for some reason with formatting, i.e. "{\" Title \ ": \" Pythons — Wikipedia \ ", \" ID \ ": 1}"

I have been suffering for a long time and can’t understand what the problem is, how can I deduce a pretty version without a slash and other extra characters?

  • one
    Your open is wrong. By default, it opens in read mode, so your code does not write anything. Try: open(a_str_path, 'w') - gil9red
  • Yes, I was wrong when writing a question. But this is no more typos. I'm just starting to work with python, so maybe there are errors in my logic. - Lyidgi
  • Can you give an example value for result from FileUtils.setData2File(result.serialize() , "test.json") ? - MaxU
  • This is the class MyData: self.m_nID = 0 - number (int) self.m_strTitle = "" - string (str) - Lyidgi
  • one
    Thanks to everyone who tried to help. A strange thing happened, after restarting Pycharm everything began to work. It looks like some of my changes for an hour had no effect on the program?!?! - Lyidgi

0