k9wan
k9wan

Reputation: 57

Is there any limitation for python pprint printing large data?

Sorry if my English is bad, I speak Korean as mother tongue.

What I was trying is to pprint a large list, of which size is like len(repr(lst)) == 64xxx, to a text file.

but when I tried something like

from pprint import pprint as p

f = open(fname, mode='a+')
p(a_lst, f)

it only printed about 80% of list, and the rest 20% were cut. and I also tried halving list and call pprint twice, but that also didn't work.

so I had to do this instead

print(a_list, file=f)

which worked. but why pprint didn't print all the list? is there some size limitation? or maybe was my RAM or etc not enough?

edit: I tried with pprint once more, and it worked and couldn't reproduce the same result (the cut result). Maybe it was temporary bug I think, but I can't come up with the cause of the bug.

Upvotes: 1

Views: 227

Answers (1)

Daviid
Daviid

Reputation: 1586

I just tried

import random

# Define the dimensions of the 2D list
first_level = 95
second_level = 24
lst = []

for _ in range(first_level):
    second_level_list = []
    for _ in range(second_level):
        # Generate a random binary string of length num_cols
        random_string = ''.join(random.choice('01') for _ in range(24))
        
        # Append the random string as a list to lst
        second_level_list.append([random_string])
    lst.append(second_level_list)

from pprint import pprint as p

f = open("output.txt", mode='a+')
p(lst, f)

and didn't get any errors.

The full array was in the output file even without doing f.close()

Upvotes: 0

Related Questions