How to break a new line of text when tweeting in a bat file

Asked 2 years ago, Updated 2 years ago, 69 views

I decided to tweet the schedule on python's tweet, and I created the following batch file using sys.argv so that I can call the py file and text it in it.

set/p Tweet="Tweet:"
set/p Time = "Time:" 
Scheduler_Tweet.py%Tweet%%Time%

This works fine when I tweet only one line, but when I wanted to tweet multiple lines, I tried to break the line with \n, but it was just tweeted as a letter.

Please let me know if there is any way to reflect a new line.
Thank you for your cooperation.

By the way, inside Scheduler_Tweet.py, you can find

auth=tweepy.OAuthHandler(CK,CS) 
auth.set_access_token(AT,AS) 
api=tweepy.API(auth)

def job():  
 api.update_status(status=(sys.argv[1]))

defmain():     
 schedule.every().day.at(sys.argv[2])) .do(job)


 while True:        
  schedule.run_pending()        
  time.sleep(1)

main()

That's it.

python windows cmd

2022-09-29 22:53

2 Answers

For example, if you create a minimal script like this and run it in a batch file instead of Scheduler_Tweet.py, you'll see what gets passed.
The \n you entered is displayed as it is.

import sys
print(sys.argv[1])
print(sys.argv[2])

And I think the article around here will be a countermeasure.
Returns the string escaped by Python
Notes on how to unescape (?) a string escaped by python
How to un-escape a backslash-escaped string?

However, simple application will disguise characters outside the ASCII range (full-width characters and half-width characters), so .encode() will apply the answers to the above English article.

If you apply it to the above script, it will look like this:

import sys
print(sys.argv[1].encode('latin1', errors='backslashreplace').decode('unicode-escape')))
print(sys.argv[2].encode('latin1', errors='backslashreplace').decode('unicode-escape')))

Scheduler_Tweet.pyTry the appropriate part of the script that retrieves the command line parameter string as described above.

< p > Reference : < / > str.encode(encoding='utf-8', errors='strict')
bytes.decode(encoding='utf-8', errors='strict')
標準エンコーディング
< " Hack " a href="/wiki/Wookieepedia " title="Wookieepedia " http : // docs. Python. org / w / 2), (4) and Library / code. html # text - to dominate the world/s d Helps " a barrel with powder " = " Does trueignore Life It became widely espoused idea with this " > < / > < / > encoding tekisuto


2022-09-29 22:53

If you pass \n as an argument for a command prompt, the string \n ("\\n" or r"\n" notation) is passed instead of the new line code \n.
You can reflect a new line by sending a new line code to tweepy.API.update_status.

sample.py

import sys
print(sys.argv[1])
print(sys.argv[1].replace("\\n", "\n")))

to sample.py above

python.\sample.py baldness The results of running are as follows:

browing\nbrowing\nbrowning\nbrowing _yomi
bark
however


2022-09-29 22:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.