Python Forum
Trying to write one line, writes 2
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to write one line, writes 2
#1
So the line below is what P is equal to. Everything works (No Errors)
Issue is, this should print in to a text file line by line

Example:
Quote:Choice1 // Choice2 // Choice3
Choice1 // Choice2 // Choice3
Choice1 // Choice2 // Choice3
Choice1 // Choice2 // Choice3


What actually happens:
Quote:Choice1
// Choice2 // Choice3
Choice1 // Choice2 // Choice3
Choice1
// Choice2 // Choice3
Choice1
// Choice2 // Choice3

p = (random.choice(lines) + " // " + PageFormat[random.randrange(0,85)] + " // " + PageType[random.randrange(0,40)] + "\n")
with open("File.txt", 'a') as l:
                    l.write(p)
Reply
#2
I guess lines comes from another file? So each line in linew has a new line char on its own. You need to strip that new line char - here or when you create lines
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 415 Feb-15-2024, 11:15 AM
Last Post: rawatg
  CSV to Text File and write a line in newline atomxkai 4 2,714 Feb-15-2022, 08:06 PM
Last Post: atomxkai
  writelines only writes one line to file gr3yali3n 2 2,388 Dec-05-2021, 10:02 PM
Last Post: gr3yali3n
  making a function that writes variables (is possible?) miker2808 3 2,362 Jan-30-2020, 06:27 PM
Last Post: buran
  write to txt file one line antmar904 3 2,275 Aug-16-2019, 02:40 PM
Last Post: Axel_Erfurt
  Python start from a specific string line and write? searching1 1 2,226 Jun-27-2019, 02:28 PM
Last Post: perfringo
  Python requests writes the name of the file instead of contents to web page bluethundr 1 2,168 Jun-05-2019, 09:35 PM
Last Post: Larz60+
  write each line of a text file into separate text files and save with different names Shameendra 3 2,793 Feb-20-2019, 09:07 AM
Last Post: buran
  Preventing useless multiple disk writes Steffenwolt 9 4,661 Jul-28-2018, 06:39 PM
Last Post: gontajones
  python export to csv writes extra line between rows jahjahcity 4 10,377 Jul-25-2018, 01:36 AM
Last Post: jahjahcity

Forum Jump:

User Panel Messages

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