Tips and Tricks to Work with Text Files in Python | Towards Data Science

Computer programs make our lives easy. We can perform a complex task within a blink of an eye with the blessings of a few lines of instructions. Among many complex tasks, working and manipulating text is one of the most important tasks done by the computer. Today, one of the hot topics is Natural Language Processing (NLP) where text processing is a must. In this article, we will discuss some basic and super easy syntax for formatting and working with texts and text files. It may be considered the first step of learning Natural Language Processing (NLP) with python. In the upcoming articles, we will discuss NLP step by step.
It’s time to move forward to our main agenda of the article. We will make our hands dirty with some basic coding examples which may help us practically. Let’s begin..........
[Full jupyter notebook link is given at the end of the article]
i. f-strings offer several benefits over the older .format() string method.For one, you can bring outside variables immediately into a string rather than pass them through as keyword arguments:
name = 'Zubair'# Using the old .format() method:print('My name is {var}.'.format(var=name))# Using f-strings:print(f'My name is {name}.')
The code generates the following outputs
If you want to have a string representation of the variable just insert !r within {}.
print(f'My name is {name!r}')
The output will be — My name is ‘Zubair’
ii. Use of f-string and dictionary.
d = {'a':123,'b':456}print(f"Address: {d['a']} Main Street")
It will show the dictionary element against the dictionary key ‘a’ . The output for the code is — Address: 123 Main Street .
[N.B. Be careful not to let quotation marks in the replacement fields conflict with the quoting used in the outer string.]
If you use only “” or ‘’, it will cause an error as above.
iii. Minimum Widths, Alignment and Padding with f-string
You can pass arguments inside a nested set of curly braces to set a minimum width for the field, the alignment and even padding characters. Consider the following codes
library = [('Author', 'Topic', 'Pages'), ('Twain', 'Rafting', 601), ('Feynman', 'Physics', 95), ('Hamilton', 'Mythology', 144)]for book in library:    print(f'{book[0]:{10}} {book[1]:{8}} {book[2]:{7}}')
Output:
Author     Topic    Pages  Twain      Rafting      601Feynman    Physics       95Hamilton   Mythology     144
Here the first three lines align, except Pages follows a default left-alignment while numbers are right-aligned. Also, the fourth line's page number is pushed to the right as Mythology exceeds the minimum field width of 8. When setting minimum field widths make sure to take the longest item into account.
To set the alignment, use the character < for left-align, ^ for center, > for right.To set padding, precede the alignment character with the padding character (- and . are common choices).
Let’s make some adjustments:
for book in library:    print(f'{book[0]:{10}} {book[1]:{10}} {book[2]:>{7}}')                                                   # here > was added
Output
Author     Topic        PagesTwain      Rafting        601Feynman    Physics         95Hamilton   Mythology      144
Centring the 3rd column
for book in library:    print(f'{book[0]:{10}} {book[1]:{10}} {book[2]:^{7}}')                                                   # here ^ was added
Output
Author     Topic       Pages Twain      Rafting      601  Feynman    Physics      95   Hamilton   Mythology    144
Adding some ....
for book in library:    print(f'{book[0]:{10}} {book[1]:{10}} {book[2]:.>{7}}')                                                # here .> was added
Output
Author     Topic      ..PagesTwain      Rafting    ....601Feynman    Physics    .....95Hamilton   Mythology  ....144
iv. Date Formatting with f-string
You can do various formatting with f-strings. An example is shown below.
from datetime import datetimetoday = datetime(year=2018, month=1, day=27)print(f'{today:%B %d, %Y}')
For more info on formatted string literals visit https://docs.python.org/3/reference/lexical_analysis.html#f-strings
i. Creating a File with IPython
This function is specific to jupyter notebooks! Alternatively, quickly create a simple .txt file with Sublime text editor.
%%writefile test.txtHello, this is a quick test file.This is the second line of the file.
The above code will create a txt file with the same directory of the Jupyter Notebook name test.txt
ii. Python Opening a File
# Open the text.txt file we created earliermy_file = open('test.txt')
You may get an error message if you mistype the file name or provide the wrong directory. So be careful. Now, read the file.
# We can now read the filemy_file.read()
Output
'Hello, this is a quick test file.\nThis is the second line of the file.'
But if you run the my_file.read() code again, it will output only ‘’ . But why?
This happens because you can imagine the reading “cursor” is at the end of the file after having read it. So there is nothing left to read. We can reset the “cursor” like this:
# Seek to the start of file (index 0)my_file.seek(0)
Now, the cursor is reset to the beginning of the text file. If we run the my_file.read() code again we will get the output ‘Hello, this is a quick test file.\nThis is the second line of the file.’ .
iii. Reading line by line
You can read a file line by line using the .readlines() method. Use caution with large files, since everything will be held in memory.
# Readlines returns a list of the lines in the filemy_file.seek(0)my_file.readlines()
Output
['Hello, this is a quick test file.\n', 'This is the second line of the file.'
iv. Writing to a File
By default, the open() function will only allow us to read the file. We need to pass the argument 'w' to write over the file. For example:
# Add a second argument to the function, 'w' which stands for write.# Passing 'w+' lets us read and write to the filemy_file = open('test.txt','w+')
Opening a file with ‘w’ or ‘w+’ *truncates the original*, meaning that anything that was in the original file **is deleted**!
# Write to the filemy_file.write('This is a new first line')
The above command writes ‘This is a new first line’ the created file.
v. Appending to a File
Passing the argument 'a' opens the file and puts the pointer at the end, so anything written is appended. Like 'w+', 'a+' lets us read and write to a file. If the file does not exist, one will be created.
my_file = open('test.txt','a+')my_file.write('\nThis line is being appended to test.txt')my_file.write('\nAnd another line here.')
The above code will append the text at the end of the existing text in the test.txt file.
vi. Aliases and Context Managers
You can assign temporary variable names as aliases, and manage the opening and closing of files automatically using a context manager:
with open('test.txt','r') as txt:    first_line = txt.readlines()[0]print(first_line)
This code will print the first sentence of test.txt file. For this time the output is This is a new first line
[N.B. The with ... as ...: context manager automatically closed test.txt after assigning the first line of text to first_line:]
If we try to read the test.txt, it will show an error message because the file has been closed automatically
vii. Iterating through a File
with open('test.txt','r') as txt:    for line in txt:        print(line, end='')  # the end='' argument removes extra linebreaks
Output
This is a new first lineThis line is being appended to test.txtAnd another line here.This is more text being appended to test.txtAnd another line here.
The syntaxes are easy but super helpful. We always jump onto sophisticated learning materials but there exist so many tiny things which can make our life easy. And the above article explains some of the useful techniques to play with text which might be helpful for text formatting. However, these techniques will be very helpful for Natural Language Processing. The write-ups will be continued and heading towards basic to advance of NLP.
The full jupyter notebook for the article is available here.
Some chosen interesting article for your further reading
towardsdatascience.com
towardsdatascience.com
If you enjoy the article, follow me on medium for more.
Connect me on LinkedIn for collaboration.