Files in Python
Files in Python
Mrs.S.Karthiga
Files
Syntax:-
file = open(“a.txt”)
file = open("t.txt","w+")
file.write("hi")
file.close()
You can also read your .txt file line by line if your data is too big to read.
This code will segregate your data in easy to ready mode
Hello.txt
Ex:-
This is line0
def main():
f= open("hello.txt","r") This is line1
f1=f.readlines()
This is line2
for x in f1:
print(x) This is line3
if __name__=="__main__":
main() This is line4
This is line5
…..
Writing multiple lines to a file at once
fh = open("hello.txt","w")
lines_of_text = ["One line of text here\n", "and another line here"]
fh.writelines(lines_of_text)
fh.close()
wordlist = wordstring.split()
wordfreq = []
for w in wordlist:
wordfreq.append(wordlist.count(w))
it was the best of times it was the worst of times it was the age of wisdom it was the
age of foolishness
List
['it', 'was', 'the', 'best', 'of', 'times', 'it', 'was','the', 'worst', 'of', 'times', 'it', 'was', 'the',
'age',
'of', 'wisdom', 'it', 'was', 'the', 'age', 'of','foolishness']
Frequencies
[4, 4, 4, 1, 4, 2, 4, 4, 4, 1, 4, 2, 4, 4, 4, 2, 4, 1, 4,4, 4, 2, 4, 1]
Pairs
[('it', 4), ('was', 4), ('the', 4), ('best', 1), ('of', 4),('times', 2), ('it', 4), ('was', 4), ('the', 4),
('worst', 1), ('of', 4), ('times', 2), ('it', 4),('was', 4), ('the', 4), ('age', 2), ('of', 4),
('wisdom', 1), ('it', 4), ('was', 4), ('the', 4),('age', 2), ('of', 4), ('foolishness', 1)]
Removing stop words Example
The process of converting data to something a computer
can understand is referred to as pre-processing. One of
the major forms of pre-processing is to filter out useless
data. In natural language processing, useless words (data),
are referred to as stop words.
Stop Words: A stop word is a commonly used word (such as
“the”, “a”, “an”, “in”) that a search engine has been
programmed to ignore
To check the list of stopwords you can type the following commands in the python shell.
import nltk
from nltk.corpus import stopwords
set(stopwords.words('english'))
{‘ourselves’, ‘hers’, ‘between’, ‘yourself’, ‘but’, ‘again’, ‘there’, ‘about’, ‘once’, ‘during’, ‘out’,
‘very’, ‘having’, ‘with’, ‘they’, ‘own’, ‘an’, ‘be’, ‘some’, ‘for’, ‘do’, ‘its’, ‘yours’, ‘such’, ‘into’,
‘of’, ‘most’, ‘itself’, ‘other’, ‘off’, ‘is’, ‘s’, ‘am’, ‘or’, ‘who’, ‘as’, ‘from’, ‘him’, ‘each’, ‘the’,
‘themselves’, ‘until’, ‘below’, ‘are’, ‘we’, ‘these’, ‘your’, ‘his’, ‘through’, ‘don’, ‘nor’, ‘me’,
‘were’, ‘her’, ‘more’, ‘himself’, ‘this’, ‘down’, ‘should’, ‘our’, ‘their’, ‘while’, ‘above’, ‘both’,
‘up’, ‘to’, ‘ours’, ‘had’, ‘she’, ‘all’, ‘no’, ‘when’, ‘at’, ‘any’, ‘before’, ‘them’, ‘same’, ‘and’,
‘been’, ‘have’, ‘in’, ‘will’, ‘on’, ‘does’, ‘yourselves’, ‘then’, ‘that’, ‘because’, ‘what’, ‘over’,
‘why’, ‘so’, ‘can’, ‘did’, ‘not’, ‘now’, ‘under’, ‘he’, ‘you’, ‘herself’, ‘has’, ‘just’, ‘where’, ‘too’,
‘only’, ‘myself’, ‘which’, ‘those’, ‘i’, ‘after’, ‘few’, ‘whom’, ‘t’, ‘being’, ‘if’, ‘theirs’, ‘my’,
‘against’, ‘a’, ‘by’, ‘doing’, ‘it’, ‘how’, ‘further’, ‘was’, ‘here’, ‘than’}
Note: You can even modify the list by adding words of your choice in the english .txt. file in the
stopwords directory.
Ex:- removing stop words
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
example_sent = "This is a sample sentence, showing off the stop words filtration."
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(example_sent)
filtered_sentence = [w for w in word_tokens if not w in stop_words]
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
print(word_tokens)
print(filtered_sentence)
Output
import sys
print (“Number of arguments:”, len(sys.argv), “arguments.”)
Print(“Argument List:”, str(sys.argv))
If you pass this in a command line Number of arguments: 7 arguments.
$ python test.py arg1 arg2 arg3 Argument List:
['main.py','$','python','test.py',
'arg1','arg2','arg3']