1
    for line in result['errors']:
        print line

This will output :

[ [ 'some text here.....', '23', '3'], 
[ 'some text here.....', '244', '4'],
[ 'some text here.....', '344', '5'] ]

Now I am using assertTrue and want to output the list:

    self.assertTrue(result['result'], 'this is error' + \
                    ', data is incorrect' + str(result['errors']))

This will output :

AssertionError: this is error, data is incorrect[ [ 'some text here.....', '23', '3'], [ 'some text here.....', '244', '4'], [ 'some text here.....', '344', '5'] ]

I need the output as below:

AssertionError: this is error, data is incorrect
[ [ 'some text here.....', '23', '3'], 
[ 'some text here.....', '244', '4'], 
[ 'some text here.....', '344', '5'] ]

How do we achieve this?

abcde
  • 65
  • 1
  • 7
  • I've changed your title and tags to reflect the underlying issue: this is about formatting a list of strings as output --- it doesn't actually have to do with the `assert` or the `AssertionError` – DilithiumMatrix Nov 17 '15 at 16:37
  • Possible duplicate of [How to print a list in Python "nicely"](http://stackoverflow.com/questions/1523660/how-to-print-a-list-in-python-nicely) – Two-Bit Alchemist Nov 17 '15 at 16:40
  • yes it about the formatting a list of strings and to output them. OK – abcde Nov 17 '15 at 16:40
  • No its a different issue with outputting error messages. Its not duplicate.. – abcde Nov 17 '15 at 16:42
  • @abcde What does the fact that you happen to be using them for error messages have to do with it? You're trying to pretty-print a list. What about the fact that you are doing error output invalidates the answer on the linked question? What about the fact that you are doing error output has any bearing on this question at all? – Two-Bit Alchemist Nov 17 '15 at 16:48

1 Answers1

1

You are converting list of lists to string

+ str(result['errors'])

thus getting one-liner, you can instead join using "\n" to get multiline

+ '\n'.join(map(str, result['errors']))

example

>>> a = [[1, 2, 3], [4, 5, 6]]
>>> 
>>> a
[[1, 2, 3], [4, 5, 6]]
>>> print a
[[1, 2, 3], [4, 5, 6]]
>>> print str(a)
[[1, 2, 3], [4, 5, 6]]
>>> print '\n'.join(map(str, a))
[1, 2, 3]
[4, 5, 6]
lejlot
  • 56,903
  • 7
  • 117
  • 144