Add quotation marks ">" for replying emails in Python3

About this article

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.

Thing you want to do

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.

E-mail sent (text of the other party)

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

Text for reply

> 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

Implementation code and execution result

The implemented code, usage, and execution example are shown below.

code

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.

How to use

  1. 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).

  2. Run reply_mark.py in the terminal as shown below.

    $ python reply_mark.py
    
  3. 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.

Input example

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

Output example

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

At the end

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

Add quotation marks ">" for replying emails in Python3
Search for strings in Python
Techniques for sorting in Python
Read Outlook emails in Python
About "for _ in range ():" in python
(For myself) Flask_8 (Add / Edit / Delete in database with python)
Check for memory leaks in Python
Check for external commands in python
Run unittests in Python (for beginners)
Add syntax highlighting for the Kv language to Spyder in the Python IDE
Notes on nfc.ContactlessFrontend () for nfcpy in python
Inject is recommended for DDD in Python
Tips for dealing with binaries in Python
Summary of various for statements in Python
Type annotations for Python2 in stub files!
Template for writing batch scripts in python
Python / dictionary> setdefault ()> Add if not in dictionary
Process multiple lists with for in Python
MongoDB for the first time in Python
Get a token for conoha in python
Sample for handling eml files in Python
AtCoder cheat sheet in python (for myself)
I searched for prime numbers in python
Notes for using python (pydev) in eclipse
Tips for making small tools in python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
Add words to MeCab's user dictionary on Ubuntu for use in Python
Template for creating command line applications in Python
++ and-cannot be used for increment / decrement in python
Import-linter was useful for layered architecture in Python
Logging settings for daily log rotation in python
Tips for hitting the ATND API in Python
Display candlesticks for FX (forex) data in Python
Boost.NumPy Tutorial for Extending Python in C ++ (Practice)
Zero padding for dynamic variable values in Python
Try to calculate RPN in Python (for beginners)
Notes for implementing simple collaborative filtering in Python
To add a module to python put in Julialang
Don't use readlines () in your Python for statement!
[Implementation for learning] Implement Stratified Sampling in Python (1)
Settings for Python coding in Visual Studio Code
Use the LibreOffice app in Python (3) Add library
[Introduction for beginners] Working with MySQL in Python
Basic story of inheritance in Python (for beginners)
Settings for getting started with MongoDB in python
2016-10-30 else for Python3> for:
Python in optimization
python [for myself]
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python