print two dimensional list

后端 未结 7 2481
[愿得一人]
[愿得一人] 2021-02-20 14:46

I have a list, in which is another list and I want to doc.write(a)

a = [[1, 2, \"hello\"],
     [3, 5, \"hi There\"],
     [5,7,\"I don\'t know\"]]
         


        
7条回答
  •  天命终不由人
    2021-02-20 15:15

    It's difficult for me to be sure, because your question is too short, but it seems to me that you are in a XY problem, that is to say :
    you ask a question about a Y problem that you think of as being the one that needs to be solved to goes out of an uphill X problem. But your real problem is that you think that the Y problem is the way to answer to the real problem X AND that you present here only the Y problem.
    Writing that, I only paraphrase what is said here: XY problem

    If I am right, my opinion is that you will have a better way to solve your real X problem using one of the following tools, that allow to serialize an object and to record the serialized object in a file:

    pickle

    marshal

    shelve

    I won't paraphrase and repeat all is the docs on these tools, read them.

    .

    If i am wrong and that you really just want to write an object under the form of a string representation, you can also do:

    from pprint import pformat
    
    a = [[1, 2, "hello"],
         [3, 5, "hi There"],
         [5,7,"I don't know"]]
    
    with open('doc.txt','w') as f:
        f.write(pformat(a,width=12))
    

提交回复
热议问题