In this article, I will introduce the code to put a quotation mark at the beginning of the other party's text when replying by e-mail etc. The writer is an amateur. I would appreciate it if you could tell me various things.
I'm not familiar with Python2, but I only know that I'm using Python3 (Is it Python3.6.0?). Therefore, the title of the article is Python3.
I would like to output the reply text with ">" at the beginning of the email (the text of the other party) sent as shown below. However, I want to leave the blank lines as they are.
Mr Sato
Hello
It is a candidate for a drinking party next week.
Store A good. a little expensive.
Store B The taste is ok. cheap.
Store C I have a lot of drinks. The atmosphere is good.
Suzuki
> Mr. Sato
> Hello
> I'm a candidate for a drinking party next week.
> Store A > Good. a little expensive.
> Store B > The taste is ok. cheap.
> Store C > I have a lot of drinks. The atmosphere is good.
> Suzuki
The implemented code, usage, and execution example are shown below.
reply_mark.py
path_r = "sent_text.txt"
path_w = "reply_text.txt"
with open(path_r) as f_r, open(path_w, mode='w') as f_w:
for line in f_r:
if len(line) > 1:
f_w.write(">" + line)
else:
f_w.write("\n")
――With open It seems that if you open a file with the writing method of ~, the file will be closed at the end of the block. (Reference: https://note.nkmk.me/python-file-io-open-with/)
--if determines whether the line is blank.
--You can also change> in f_w.write (">" + line) to another symbol, etc. according to your needs.
Create a file called sent_text.txt in the directory where reply_mark.py is located, and copy and paste the sent mail (the text of the other party).
Run reply_mark.py in the terminal as shown below.
$ python reply_mark.py
A file called reply_text.txt is created (*), and the reply text with ">" at the beginning of the other party's text is output. When using it, copy it from here.
sent_text.txt
Mr Sato
Hello
It is a candidate for a drinking party next week.
Store A
good. a little expensive.
Store B
The taste is ok. cheap.
Store C
I have a lot of drinks. The atmosphere is good.
Suzuki
reply_text.txt
>Mr Sato
>Hello
>It is a candidate for a drinking party next week.
>Store A
>good. a little expensive.
>Store B
>The taste is ok. cheap.
>Store C
>I have a lot of drinks. The atmosphere is good.
>Suzuki
I want to make it a little more convenient. Ideally, if you press "command + C" twice in the DeepL app, you'll get results.
Also, I feel that I can do the same thing with a shell script, but it's okay because I'm also studying Python.
If you have any questions, please point out and ask questions. In particular, I think it will be useful to study if there are suggestions for improving how to write code and how to create a mechanism.
Recommended Posts