Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

saving file with format

Hi guys I am trying to save my file in python…
I dont know how to do it.. It came out the save file to be like this :
506882122734561843241851186242872

What I am trying to do is
["50688", "212273", "4561843", "241851", "18624", "2872"]

with open("output_data.txt", "w") as out_file:
    for user in users:
        out_string = ""
        out_string += str(user)
        out_file.write(out_string)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The loop you’re using is stitching together each of the elements in your users list so you end up with one long string in out_string. You probably introduced this loop after you got an error message when trying to save the list straight to a file.

Instead, and as suggested in the comments, you could save the data as JSON:

import json

users = ["50688", "212273", "4561843", "241851", "18624", "2872"]

with open("output_data.txt", "w") as out_file:
    out_file.write(json.dumps(users))

output_data.txt will then contain:

["50688", "212273", "4561843", "241851", "18624", "2872"]

Note: this assumes that your original list is a list of strings, not integers (it wasn’t clear from your question).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading