0

I am getting this:

0.0  angry27.56%
0.0  disgust0.0%
0.0  fear18.75%
0.0  happy14.47%
0.0  sad5.34%
0.0  surprise14.96%
0.0  neutral18.92%

I want to get this:

0.0  angry27.56%, disgust0.0%, fear18.75%, happy14.47%, sad5.34%, surprise14.96%, neutral18.92%

I am using this code:

emotion = ""
        for i in range(len(predictions[0])):
            emotion = "%s%s%s" % (emotions[i], round(predictions[0][i]*100, 2), '%')
            print(str(sec)+ "  " + emotion)
Dilshad
  • 1
  • 2
  • Does this answer your question? [Print to the same line and not a new line in python](https://stackoverflow.com/questions/3419984/print-to-the-same-line-and-not-a-new-line-in-python) – Antonin GAVREL Jun 29 '20 at 05:02

2 Answers2

1
print(str(sec)+ "  " + emotion,end=', ')

end=' ' parameter inside the print statement can be used to print output in a single line....

Srikeshram
  • 87
  • 1
  • 5
  • its a duplicate of https://stackoverflow.com/questions/3419984/print-to-the-same-line-and-not-a-new-line-in-python?r=SearchResults – Antonin GAVREL Jun 29 '20 at 05:01
  • I tried it but all in vain!!! Now I am getting 0.0 angry 27.56% 0.0 disgust 0.0% 0.0 fear 18.75% 0.0 happy 14.47% 0.0 sad 5.34% 0.0 surprise 14.96% 0.0 neutral 18.92% 0.04 angry 1.0% 0.04 disgust 0.0% 0.04 fear 5.16% 0.04 happy 1.97% 0.04 sad 1.19% 0.04 surprise 5.57% 0.04 neutral 85.12% 0.08 angry 0.51% 0.08 disgust 0.0% 0.08 fear 5.36% 0.08 happy 0.3% 0.08 sad 1.27% 0.08 surprise 5.47% 0.08 neutral 87.08% 0.12 angry 0.82% 0.12 disgust 0.0% 0.12 fear 5.5% 0.12 happy 0.15% 0.12 sad 2.65% 0.12 surprise 2.45% 0.12 neutral 88.42% – Dilshad Jun 29 '20 at 05:03
  • Use `end=', '` comma and space inside single quote and execute it again... – Srikeshram Jun 29 '20 at 05:08
1

First create a empty list then append all values of emotion in it then use join() to combine elements of list :

emotion = ""
res = []
for i in range(len(predictions[0])):
    emotion = "%s%s%s" % (emotions[i], round(predictions[0][i] * 100, 2), '%')
    res.append(emotion)
print(str(sec), end=" ")
print(", ".join(res))
Shivam.k
  • 11
  • 3