I am trying to write down a CSV using python with multiple lines inside the same cell. For example i want the next result:
But I am getting this result:
I have tried several ways to insert a "\n" between both elements of the cell but not working. My last attempt was the next piece of code:
f=open("prueba.csv","a",newline="")
header=["Prueba","Prueba2"]
writer=csv.writer(f)
writer.writerow(header)
prueba11="123"
prueba21="123"
prueba22="124"
prueba1=prueba11
prueba2=prueba21 "\n" (prueba22)
prueba_write2=str(prueba2)
prueba_write1=str(prueba1)
row=[prueba_write1,prueba_write2]
writer.writerow(row)
f.close()
Does somebody know if it is a easy way to get the desired result? Thanks!
CodePudding user response:
please try:
prueba2=str(f'{prueba21}\n{prueba22}')
should result:
If you are concatenating variables with strings, try to use f"Hello {variable}"
(fstrings) - recommended or "Hello, %s." % variable
.
If you search python fstrings or string formating for python on google, you`ll have a better idea than i wrote here..
Also, if you have time, check this article/chapter from the book "Automate the Boring Stuff with Python" by Al Sweigart ->