<jupyter_start><jupyter_text>Working with pdf and text files for NLP  String Formatter<jupyter_code>name = "<NAME>"
print('The name of the person is {}'.format(name))
print(f'The name of the person is {name}')
# minimum width and alignment b/w columns
mylist = [('tuple 1',12),
         ('tuple 2',11),
         ('phirse tuple',10),
         ('ek aur tuple', 23),
         ('last tuple', 12)]
mylist
for i in mylist:
    print (i)
#formatting
for i in mylist:
    print (f'{i[0]:{50}} {i[1]:{10}}')
#>,<,^ for alignment
for i in mylist:
    print (f'{i[0]:<{50}} {i[1]:.>{10}}')<jupyter_output>tuple 1                                            ........12
tuple 2                                            ........11
phirse tuple                                       ........10
ek aur tuple                                       ........23
last tuple                                         ........12<jupyter_text>Working w .CSV and .TSV files<jupyter_code>import pandas as pd
data = pd.read_csv('moviereviews.tsv',sep = '\t')
data.head()
data.shape
data['label'].value_counts()
pos = data[data['label'] == 'pos']
pos.head()
pos.to_csv('pos.tsv', sep = '\t', index = False)
pd.read_csv('pos.tsv', sep = '\t').head()
%%writefile text1.txt
hello, writing with magic command.
This is line two.
#built in magic command
%%writefile -a text1.txt
Appending New Line<jupyter_output>Appending to text1.txt<jupyter_text>Using Inbuilt Commands<jupyter_code>file = open('text1.txt','r')
file
file.read()
file.read()
file.seek(0)
file.read()
file.readline()
file.seek(0)
file.readlines()
file.close()
with open('text1.txt') as file:
    text_data = file.readlines()
    print(text_data)
for temp in text_data:
    print(temp.strip())
for i, temp in enumerate(text_data):
    print(str(i) + "--->" + temp.strip())
## Write file
file = open('text2.txt','w')
file
file.write("This is just another file")
#to complete write operation
file.close()
with open('text3.txt','w') as file:
    file.write('This is third file \n')
with open('text3.txt','a') as file:
    for temp in text_data:
        file.write(temp)<jupyter_output><empty_output><jupyter_text>Text Extraction from PDF files<jupyter_code>!pip install PyPDF2
import PyPDF2 as pdf
file = open('NLP.pdf','rb')
file
pdf_reader = pdf.PdfFileReader(file)
pdf_reader
help(pdf_reader)
pdf_reader.getIsEncrypted()
pdf_reader.getNumPages()
page4 = pdf_reader.getPage(3)
page4.extractText()
page5 = pdf_reader.getPage(4)
page5.extractText()<jupyter_output><empty_output><jupyter_text>Append and Merge PDFs<jupyter_code>pdf_writer = pdf.PdfFileWriter()
pdf_writer.addPage(page4)
pdf_writer.addPage(page5)
output = open('Pages.pdf','wb')
pdf_writer.write(output)
output.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16058394160583941, 'fraction_numerical': 0.020437956204379562, 'mean_word_length': 3.269470404984424, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '448859', 'license': 'MIT', 'n_tokens_mistral': 975, 'n_tokens_neox': 947, 'n_words': 232, 'path': 'Working with text and pdf files/Working with pdf and text files.ipynb', 'repo_name': 'gauraverma33/Everything-NLP', 'size': '2744'}