0

I have a list of dicts in Python and want to upload it as a json-file to Azure File Storage. When I print the list locally the linebreaks exist. After uploading and manually checking the file on Azure File Storage I noticed that the linebreaks were non existent.

list_of_dicts = my_json_dicts
transformed_dict_str = '\n'.join([json.dumps(x) for x in list_of_dicts])

# print(transformed_dict_str) gives me the "dicts"/lines separated by linebreaks.

service.create_file_from_text(share_name, file_path, file_name.json, transformed_dict_str, encoding='utf-8')

Can anyone tell me why the uploaded file (when i open it in notepad after downloading manually via the browser interface of Azure) does not contain any linebreaks?


Edit:

When I write the string to a local path with the following code, the linebreaks still exist. So it must happen during the create_file_from_text function?

file = open("myjson.json", "w")
file.write(transformed_dict_str)
file.close()
Cribber
  • 2,513
  • 2
  • 21
  • 60

1 Answers1

1

Please use '\r\n' instead of '\n' in your code.

I can reproduce your issue when use '\n', but works fine using '\r\n' (in notepad, there is linebreaks).

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • It works perfectly, thanks! Can you explain why newline (\n) needs return (\r) as well in this setting? – Cribber Jan 11 '19 at 09:39
  • 1
    hi @Cribber, you can take a look at this [so](https://stackoverflow.com/questions/3821784/whats-the-difference-between-n-and-r-n) thread for the detailed explanation. – Ivan Glasenberg Jan 11 '19 at 09:44