Transform an array to string in python

Asked

Viewed 2,749 times

4

I am trying to convert an array to a string and add "|" to it at the beginning and end of each field

Below the example array

['', 'C170', '1', '14879', '', '1,00000', 'UN', '29,99', '0,00', '1', '060', '1407',
 'NE09', '0,00', '0,00', '0,00', '0,00', '0,00', '0,00', '0', '49', '', '0,00', '0,00',
 '0,00', '99', '0,00', '0,00', '', '', '0,00', '99', '0,00', '0,00', '', '', '0,00', 
'3010107010057', '\n']

1 answer

3


"|".join(a)

produces a string with elements separated by "|"

'|C170|1|14879||1,00000|UN|29,99|0,00|1|..... 3010107010057|\n'

Joining "|" at beginning and end:

"|" +  "|".join(a) + "|"

or

 "|%s|" %   "|".join(a)

gives

'||C170|1|14879||1,00000|UN|29,99|0,00|1|..... 3010107010057|\n|'

this is what you want?

  • 1

    Thanks was just what was needing rsrsrs :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.