Python Forum
coma separator is printed on a new line for some reason
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
coma separator is printed on a new line for some reason
#1
Gentlemen!
Ladies if any?!

I have never seen it before, Confused I'm formatting a string, separating vars by a coma, and for some reason, the last coma is printed on a new line...

print(f"{str(starttime)},{str(endtime)},{str(cell_nm)},{str(hand_nm)},{str(tt)},{str(prod)},") 
I made sure the VARs all are sctrings but still last come is printed on a new line...
I even tried this:
        coma = ","                   
        print(f" PRODUCT = {prod}{coma}")
But still, the coma printed on a new line... Cry
I'm confused how to go around it...

Any help is appreciated.
Thank you!
Reply
#2
Dear God!
The problem was in Var {prod}.
Here how I fixed it:
                            ss = f"{starttime},{endtime},{cell_nm},{hand_nm},{tt},{prod}" 
                            ss = ss.strip()                          
                            print(f"** {ss},")
Anyway, I'm glad I solved it.
Thank you!
Have a good night.... Smile
Reply
#3
I guess prod has at the end of the str a newline character.
You can strip it. I often use rstrip to remove only on the right side of the str whitespace. A newline character is also seen as a whitespace.

delimiter = ","
product_name = "Test 123"
product_name_newline = "Test 123\n"



print("Test without newline")
print(f"{product_name}{delimiter}")
print()

print("Test with newline at the end of the str")
print(f"{product_name_newline}{delimiter}")
print()

print("Test with newline at the end of the str and the prevention")
print(f"{product_name_newline.rstrip()}{delimiter}")
print()
tester_V likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
This is a problem you should figure out yourself. Knowing that the newline must be coming from somewhere the first thing I would try is this:
print(f"{prod},")
print(f"{prod.strip()},"
I would expect the first print to have the last comma on a new line, and the second to have the last comma on the same line. Now you know that the prod str has a trailing newline character.

You should study the print command and f'string formatting. There was no need for the str() here:
print(f"{str(starttime)},{str(endtime)},{str(cell_nm)},{str(hand_nm)},{str(tt)},{str(prod)},") 
f'string formatting converts in brackets to a str it it is not already a str.
print(f"{starttime},{endtime},{cell_nm},{hand_nm},{tt},{prod},") 
The above will give the exact same results.

But there is no need for the f'string formatting either.
print(starttime, endtime, cell_nm, hand_nm, tt, prod, '', sep=',') 
Also gives the exact same results.
tester_V likes this post
Reply
#5
Thank you again!
I got it...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing a raw string with a folder separator at the end, duplicates the separator krulah 5 1,228 Nov-28-2022, 12:41 PM
Last Post: snippsat
  How can histogram bins be separated and reduce number of labels printed on x-axis? cadena 1 895 Sep-07-2022, 09:47 AM
Last Post: Larz60+
  Numpy savetxt, how save number with decimal separator SpongeB0B 1 3,226 May-10-2020, 01:05 PM
Last Post: ThomasL
  Reason of the output himanshub7 2 2,185 Apr-28-2020, 10:44 AM
Last Post: TomToad
  Reverse printed words hindubiceps 7 3,006 Apr-21-2020, 05:19 PM
Last Post: deanhystad
  Code used to work, not anymore for no apparent reason? chicagoxjl 1 1,901 Jan-08-2020, 05:05 PM
Last Post: jefsummers
  I get "None" at the end of my printed result. dyshkant 3 2,664 Sep-06-2019, 06:31 PM
Last Post: dyshkant
  bytes not being printed as expected Skaperen 2 2,380 Aug-27-2019, 05:33 AM
Last Post: Skaperen
  Syntax error for a reason I don't understand DanielCook 2 2,453 Aug-07-2019, 11:49 AM
Last Post: buran
  Change linenumber and filename printed in exceptions like #line in C kryptomatrix 2 2,681 Jul-12-2019, 06:01 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020