mercredi 28 janvier 2015

Python list not printing as desired


Vote count:

0




I have a list, which is obtained as an output from a separate Python regex part of the code. The list is as follows:



v = [['-0.00162439495203', '-0.000178892778126']]


I am trying to convert this to string and write to a file as



v_new = ','.join(map(str, v))
f.write('%s\n'%v_new)
f.close


However, when I open my file instead of having



-0.00162439495203
-0.000178892778126


I get ['-0.00162439495203', '-0.000178892778126'], that is, including the [] and '' and in the same line. Note the file f is opened for writing else where, which is not shown here.






asked 5 mins ago


2 Answers



Vote count:

0




You have a list within a list, which contains a string. If you want to extract the string from that list, simply index it twice:



f.write('{0}'.format(v[0][0]) )


answered 38 secs ago



Vote count:

0




If you specify the element of v, a string will print without the brackets and quotes. Like so:



v = [['-0.00162439495203']]
v_new = ','.join(map(str, v[0]))


answered just now






Python list not printing as desired

Aucun commentaire:

Enregistrer un commentaire