2

I have an ugly list of lists generated in a program, which looks like this:

 a= [[93.400000000000006, "high"], [98.600000000000009, 99.0, "high"], [121.30000000000001, 124.1000000000000]]

I am saving it to a text file as follows:

with open('sample.txt','a') as outfile:
    json.dump(a,outfile)
    outfile.write("\n\n")

When I open the text file, the values saved are an eyesore. How do I save each list to a new line?

For example if I wanted to print each list to a new line, I could simply do:

for i in range(len(a)):
    print a[i]

thank you

EDIT: OUTPUT HAS TO LOOK LIKE :

[93.400000000000006, "high"]
[98.600000000000009, 99.0, "high"]

i.e each on one line.

11
  • 2
    Is there a reason you're using json.dumps? Commented Mar 15, 2018 at 20:47
  • 1
    you don't have a list of tuples to start with… Commented Mar 15, 2018 at 20:47
  • 1
    It depends on how you want to store the values. Do you want just to save them like this [93.400000000000006, "high"] or like this 93.400000000000006,"high". Pleasge give us an example how your output should look like Commented Mar 15, 2018 at 20:49
  • Possible duplicate of how would i write to file with python Commented Mar 15, 2018 at 20:50
  • @ChristianDean only reason: I know that we have to use json.dump for saving json data. now I had to save this list, so I json.dump was only thing I knew, so I tried and it gave no errors. Now based on the answers I understand that outfile.write with for loop can do the job. :) Commented Mar 15, 2018 at 21:18

4 Answers 4

4

I really don't any reason here to use json.dumps. You can just use a normal for loop:

a = [
        [93.400000000000006, "high"], 
        [98.600000000000009, 99.0, "high"], 
        [111.60000000000001, 112.5, "high"]
]

with open('sample.txt', 'a') as outfile:
    for sublist in a:
        outfile.write('{}\n'.format(sublist))

The above code produces the output:

[93.400000000000006, 'high']
[98.600000000000009, 99.0, 'high']
[111.60000000000001, 112.5, 'high']
Sign up to request clarification or add additional context in comments.

Comments

1

You can try:

with open('sample.txt','a') as outfile:
    for item in a:
        json.dump(item,outfile)
        outfile.write("\n\n")

Comments

1

If you want the output to be in JSON format, but want the elements of the outer list to appear on separate lines in the file, supply the indent keyword argument:

with open('sample.txt','w') as outfile:
    json.dump(a, outfile, indent=4)

Given a number or a whitespace string, indent specifies the indentation to use at each level, showing the structure of the data. By default, the output is not pretty-printed at all, to save space. See the documentation for details. The same argument works with json.dumps to create a string.

Note that the JSON data format is not designed to be "embedded" in other files; you should not use the 'a' file mode for writing JSON, and should not plan on the file containing anything else besides your one JSON value.

Another option is the pprint standard library module, if you aren't especially concerned with the data format. pprint.pprint will automatically decide how to split up lines based on their length:

>>> import pprint
>>> pprint.pprint([[1],[2],[3]])
[[1], [2], [3]]
>>> pprint.pprint([[1],[2],[3]], width=1) # force line-wrapping
[[1],
 [2],
 [3]]

It can print to a file by specifying the stream argument.

Finally, it is worth noting the option of iterating over the list and using json.dump for each line. This does not produce valid JSON, but instead a related data format called JSONL.

Comments

0

this should work for you:

file=open('sample.txt','w')
for i in a:
    file.write(str(a))
    file.write("\n\n")

Comments

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.