Skip to content

Commit 3fb12f2

Browse files
Read/Write File
1 parent 77b5c49 commit 3fb12f2

File tree

1 file changed

+159
-28
lines changed

1 file changed

+159
-28
lines changed

p18.py

Lines changed: 159 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -259,19 +259,19 @@
259259

260260

261261

262-
# Excercise 1
263-
with open("file1.txt","r") as rf:
264-
with open("file2.txt","w") as wf:
265-
for i in rf:
266-
wf.write((f"{(i).split(',')[0]}\'s salary is {(i).split(',')[1]}"))
262+
# # Excercise 1
263+
# with open("file1.txt","r") as rf:
264+
# with open("file2.txt","w") as wf:
265+
# for i in rf:
266+
# wf.write((f"{(i).split(',')[0]}\'s salary is {(i).split(',')[1]}"))
267267

268268

269-
# OR
270-
with open("file1.txt","r") as rf:
271-
with open("file2.txt","w") as wf:
272-
for line in rf.readlines():
273-
name, salary=line.split(',')
274-
wf.write(f'{name}\'s salary is {salary}')
269+
# # OR
270+
# with open("file1.txt","r") as rf:
271+
# with open("file2.txt","w") as wf:
272+
# for line in rf.readlines():
273+
# name, salary=line.split(',')
274+
# wf.write(f'{name}\'s salary is {salary}')
275275

276276

277277

@@ -284,7 +284,7 @@
284284

285285

286286

287-
# Excercise 2
287+
# # Excercise 2
288288
# with open("index.html","r") as rf:
289289
# with open("link.txt","w") as wf:
290290
# for line in rf.readlines():
@@ -299,20 +299,151 @@
299299

300300

301301

302-
# Better way
303-
with open("index.html","r") as rf:
304-
with open("link.txt","w") as wf:
305-
page=rf.read()
306-
# link_exist=True
307-
while True:
308-
pos=page.find("<a href=")
309-
if pos== -1:
310-
break
311-
# link_exist=False
312-
else:
313-
first_quot=page.find("\"",pos)
314-
second_quot=page.find("\"",first_quot+1)
315-
web=page[first_quot+1:second_quot]
316-
wf.write(web+"\n")
317-
page=page[second_quot:]
302+
# # Better way
303+
# with open("index.html","r") as rf:
304+
# with open("link.txt","w") as wf:
305+
# page=rf.read()
306+
# # link_exist=True
307+
# while True:
308+
# pos=page.find("<a href=")
309+
# if pos== -1:
310+
# break
311+
# # link_exist=False
312+
# else:
313+
# first_quot=page.find("\"",pos)
314+
# second_quot=page.find("\"",first_quot+1)
315+
# web=page[first_quot+1:second_quot]
316+
# wf.write(web+"\n")
317+
# page=page[second_quot:]
318+
319+
320+
321+
322+
323+
324+
325+
326+
327+
328+
329+
330+
331+
332+
333+
334+
335+
# # Read Love Story
336+
# with open('file1.txt','r') as rf:
337+
# print(rf.encoding)
338+
# data=rf.read()
339+
# print(data)
340+
341+
# # # file1.txt
342+
# # Love Story
343+
# # chat start
344+
# # 😘😗😇💓😗
345+
# # chat end
346+
# # Note - Real friends don't need to send this emojis !
347+
# # One more Note - after your family, dogs love human being most but they can't send you emojis !
348+
# # Second last note - if someone sends you thse emojis just say :) and don't get over exited !
349+
# # Fissl Note - I love you, subscribe to this channel now !
350+
351+
# # o/p: cp1252
352+
# # Love Story
353+
# # chat start
354+
# # 😘😗😇💓😗
355+
# # chat end
356+
# # Note - Real friends don't need to send this emojis !
357+
# # One more Note - after your family, dogs love human being most but they can't send you emojis !
358+
# # Second last note - if someone sends you thse emojis just say :) and don't get over exited !
359+
# # Fissl Note - I love you, subscribe to this channel now !
360+
361+
362+
363+
# # to read emojis we should use encoding
364+
# with open('file1.txt','r',encoding='utf-8') as rf:
365+
# print(rf.encoding)
366+
# data=rf.read()
367+
# print(data)
368+
369+
# # o/p: utf-8
370+
# # Love Story
371+
# # chat start
372+
# # �����😇💓💓😗😗
373+
# # chat end
374+
# # Note - Real friends don't need to send this emojis !
375+
# # One more Note - after your family, dogs love human being most but they can't send you emojis !
376+
# # Second last note - if someone sends you thse emojis just say :) and don't get over exited !
377+
# # Fissl Note - I love you, subscribe to this channel now !
378+
379+
380+
381+
382+
383+
384+
385+
386+
387+
# # if our file has number of char and if we read then it will not memory efficient
388+
# with open('file1.txt','r') as rf:
389+
# data=rf.read(100)
390+
# print(data)
391+
392+
# # o/p: it will return only 100 char from file
393+
394+
395+
396+
# # to read whole file
397+
# with open('file1.txt','r') as rf:
398+
# data=rf.read(10)
399+
# while(len(data)>0):
400+
# print(data)
401+
# data=rf.read(10)
402+
403+
404+
405+
406+
407+
408+
409+
410+
411+
412+
413+
414+
415+
416+
417+
418+
419+
420+
# # Work with CSV File
421+
# # csv file ---> used for storing data in tabula form
422+
# # we can read csv file using reader function or DictWriter class from csv module
423+
# from csv import reader
424+
# with open('file3.csv','r') as rf:
425+
# csv_reader=reader(rf)
426+
# # csv_reader is iterator
427+
# print(csv_reader) # o/p: <_csv.reader object at 0x0125D8E8>
428+
# print(type(csv_reader)) # o/p: <class '_csv.reader'>
429+
# for i in csv_reader: # csv_reader is iterator so we can print it only one time, if we want to use more than one time thwn we can conver it into list.
430+
# print(i)
431+
432+
# # o/p: ['name', 'email', 'phone_no'] # but here column name also printed, we can overcome this problem using next() methos
433+
# # ['Ravin', '[email protected]', '+918732904392']
434+
# # ['Rupen', '[email protected]', '+917433089863']
435+
436+
437+
438+
318439

440+
from csv import reader
441+
with open(r'file3.csv','r',encoding='utf=8') as rf:
442+
csv_reader=reader(rf)
443+
# csv_reader is iterator
444+
print(csv_reader) # o/p: <_csv.reader object at 0x0125D8E8>
445+
print(type(csv_reader)) # o/p: <class '_csv.reader'>
446+
next(csv_reader)
447+
for i in csv_reader: # o/p: ['Ravin', '[email protected]', '+918732904392']
448+
print(i) # ['Rupen', '[email protected]', '+917433089863']
449+

0 commit comments

Comments
 (0)