Chapter -12 (Text files)
For learn how to download and install python link here: How to download and install python in window
For learn how to run and execute the code in python link here:How to write and run the code
In this blog, the format is first I write Code then Result of that code. You can directly copy the code and so your time save in written that code. Also , image of the code and result and after which it contain explanation of the code.
Example-1: First of all save a notepad file with some content on your desktop and gave name whatever you want to the file. In my case I save a file on desktop and gave name 'python' to this file.
Convert all the text of file 'Python' into list.
Code:
lines = [line.strip() for line in open('python.txt')]
print(lines)
Result:
['This is line 1.', 'This is line 2.']
Explanation:- Use strip to convert all the content into list. If you use split instead of strip then it make all word into list like [['This', 'is', 'line', '1.'], ['This', 'is', 'line', '2.']]
Example-2: Convert all the text of file 'Python' into string.
Code:
s = open('python.txt').read()
print(s)
Result:
This is line 1.
This is line 2.
Explanation:- This command you see in the previous chapter.
Example-3: In previous two example you see first you save file on your desktop but if you want to open file from other drive. Then complete specify the path.
Code:
s= open('C:/Users/Guru/Desktop/python.txt').read()
print(s)
Result:
This is line 1.
This is line 2.
Explanation:- Here you see the complete path of the file. Remember here not use backslash(\) here use forward slash so use forward slash otherwise it show error.
Example-4: Write something in the text file by the help of python.
Code:
f = open('result.txt', 'w')
print('This is line 1.', file=f)
print('This is line 2.', file=f)
f.close()
Result:
This is line 1
This is line 2
Explanation:- In the first line we first open the file and then 'w' which is used to write. In the 2nd and 3rd line we use the content which we want to write in the file, then close the file.
< Result not seen directly if you open that file already, close that file and again open then you see the result in that file>
Example-5: Take the temperature data from one file and then convert into Fahrenheit and save the value to another file.(eg. 63,89) are the temperature value.
Code:
file1 = open('result.txt', 'w')
temperatures = [line.strip() for line in open('temp.txt')]
for t in temperatures:
print(int(t)*9/5+32, file=file1)
file1.close()
Result:
145.4
192.2
Explanation:- First we open that file which contain temperature value then convert into list and then use formula to change the temperature in degree into Fahrenheit.
Example-6: Create a file that contains result of every tennis match result. This text file looks like:
02/27/2010, Robert Morris, 61, Mount St. Mary's, 63
12/20/2015, Robert , 65 , St. Mount , 98
Code:
games = [line.split(',') for line in open('temp.txt')]
print(games[0])
print(max([abs(int(g[2])-int(g[4])) for g in games]))
Result:
['02/27/2010', ' Robert Morris', ' 61', " Mount St. Mary's", ' 63\n']
33
Explanation:- In the first line you see split command that separate the two list.
In 2nd line, games[0] which show that first list is the 02/27/2010...... and second list is 12/20/2015....... , so in result it print the first list i.e., 02/27/2010
In the 3rd line we use code in which g[2] is 61 and g[4] is 63 in first list and in the second list g[2] is 65 and g[4] is 98 then it show the difference which is greater in between them then it print the largest difference.
Example-7: Write a program and find the game info in which the player win by largest difference.This text file looks like:
02/27/2010, Robert Morris, 61, Mount St. Mary's, 63
12/20/2015, Robert , 65 , St. Mount , 98
Code:
lines = [line.strip() for line in open('temp.txt')]
games = [line.split(',') for line in lines]
big_diff = 0
for g in games:
diff = abs(int(g[2])-int(g[4]))
if diff>biggest_diff:
big_diff = diff
print(g)
Result:
['12/20/2015', 'Robert' , '65 ', 'St. Mount' , '98']
Explanation:- In the previous example you see the largest difference win but in this we give the info of game in which difference is maximum.
Example-8: First of all save a file with some text. In my case I save the text in 'temp' file.
< Python is one of the most popular programming language.>
Write a program in which find the word which length greater than 5 in the save text file.
Code:
wordlist= open('temp.txt').read()
x=wordlist.split()
y=[w for w in x if len(w)>5]
print(y)
Result:
['Python', 'popular', 'programming', 'language.']
Explanation:- First we convert all the text into list and then find the word which length greater than 5.
Example-9: First of all save a file with some text. In my case I save the text in 'temp' file.
< Python is one of the most popular programming language.>
Write a program and find all word which start by 'on' or 'th'.
Code:
wordlist= open('temp.txt').read()
x=wordlist.split()
y=[w for w in x if w[:2]=='on' or w[:2]=='th']
print(y)
Result:
['one', 'the']
Explanation:- In the 3rd line you see w[:2] which means first two word of the list then it find the word and print the result.
Example-10: First of all save a file with some text. In my case I save the text in 'temp' file.
< Python is one of the most popular programming language.>
Write a program and find the word if these word start by vowel.
Code:
wordlist= open('temp.txt').read()
x=wordlist.split()
count=0
for c in x:
if c[0]=='a' or c[0]=='e' or c[0]=='i' or c[0]=='o' or c[0]=='u':
count=count+1
print(c)
Result:
is
one
of
Explanation:- You easily understood this code if you read previous chapter.
Example-11: First of all save a file with some text. In my case I save the text in 'temp' file.
< Python is one of the most popular programming language.>
Write a program and find the word which start by 'pr' and end by 'ng'.
Code:
wordlist= open('temp.txt').read()
x=wordlist.split()
for c in x:
if c[:2]=='pr' and c[-2:]=='ng':
print(c)
Result:
programming
Explanation:- In the example 10 you see the word which start by vowel so we use 'or' command but here use 'and' command.
Example-12: Write a program and print some text into the file.
Code:
s="Hello World"
x=str(s)
z=open('result.txt' , 'w')
print(x, file=z)
z.close()
Result:
Hello World
Explanation:- First we write some text in first line then convert into string then we open the text file and use 'w' command and then close the file.
Example-13: First of all save a file with some text. In my case I save the text in 'temp' file.
< Python is one of the most popular programming language.>
Write a program and print the largest word of the text file.
Code:
wordlist=open('temp.txt').read()
z=wordlist.split()
x=sorted(z, key=len)
print(x[-1])
Result:
programming
Explanation:- Here in 3rd line use sorted which is use to sort the word according to length and then use x[-1] which means last word of list i.e. largest word.
No comments:
Post a Comment
If you have any doubt, let me know.