error handling in python | Lec-23

  Рет қаралды 1,927

MANISH KUMAR

MANISH KUMAR

Күн бұрын

Пікірлер: 12
@ShubhamRai06
@ShubhamRai06 6 ай бұрын
very well explained.
@vishnu000001
@vishnu000001 7 ай бұрын
Hi Manish, def find_distinct_name(file_path,action): try: file = open(file_path, action) lines = file.read().splitlines() name = [] if len(lines)==0: raise Exception("the file is empty") else: for i in range(1,len(lines)): line = lines[i] words = line.split(",") name.append(words[1]) name_list = set(name) print(name_list) except Exception as e: print(e) find_distinct_name("details.txt", "r") o/p = {'"Manish"', '"Rohan"', '"Simaran"', '"Manish'} when reading the file using read option. All the lines are getting converted into strings and when i use split method to get list from lines. I am getting data as "Manish" and "Manish, instead of error. Please let me know if there is a way to read data from txt file along with data type.
@sharadsonwane6594
@sharadsonwane6594 7 ай бұрын
👍
@bhargavnagineni9322
@bhargavnagineni9322 7 ай бұрын
Hi bro.. can you please share the topics required in python for data engineer
@parthmm
@parthmm 7 ай бұрын
sir how to practice python for data analyst
@milindchavan4688
@milindchavan4688 Ай бұрын
from loguru import logger import os temp_file='tempfile.txt' def log(**logs): if os.path.exists('D:\Python Programming'): append_write = 'a' else: append_write = 'w' matter=open(temp_file,append_write) for key,value in logs.items(): matter.write(key+' '+value+' '+' ') matter.close() num_entries = int(input('Enter the number of entries you want to add: ')) user_dict={} for i in range(num_entries): key = input('Enter key:') value = input('Enter value:') user_dict[key] = value log(**user_dict)
@abhijeetsinghbatra8143
@abhijeetsinghbatra8143 3 ай бұрын
def cal(): try: li = [] with open('file.txt','r') as handler: next(handler) fc = handler.read().strip() if not fc: raise ValueError('File is empty') for i in fc.splitlines(): st = i.split(',')[1] if not st.startswith("'") and not st.endswith('"'): print(st) raise ValueError("Name not quoted") tt = st.replace('"','') li.append(tt) print(list(set(li))) except FileNotFoundError: print('FIle is wrong') raise Exception("Please check path") except TypeError: print('Type Error') raise TypeError("Plx check type") except Exception as e: print(e) else: print('here') finally: print('OOS') try: cal() except Exception as e: print(e)
@hardeepcharak
@hardeepcharak 7 ай бұрын
Is that you in DP?
@ankitdhurbey7093
@ankitdhurbey7093 7 ай бұрын
import pandas as pd def unique_values(path, column_name): try: df = pd.read_csv(path) name = df[f"{column_name}"] return set(name) except FileNotFoundError: print("The File is not available at given path") raise Exception("Please check the path provided") except KeyError: raise Exception("Please provide the correct column name") except Exception as e: if (str(e) == 'No columns to parse from file'): raiseException('The file is empty, No columns found') try: output = unique_values(path="C:/Desktop/NewFolder/names.csv",column_name="name") except Exception as e: print(e) print(f"The list of unique values are : {list(output)}")
@VivekKhare-z1m
@VivekKhare-z1m 4 ай бұрын
file_path = "assignment6.txt" unique_name = set() try: with open(file_path, 'r') as file: file_contents = file.read().strip() if not file_contents: raise ValueError("File is empty it doesn't contain any data") else: for line in file_contents.splitlines(): if line.startswith("id,name,age,gender"): continue parts = line.split(',') name = parts[1].strip('"') unique_name.add(name) logger.info(f"Unique names in the file: {unique_name}") except Exception as e: logger.info(e)
@safiadawood5637
@safiadawood5637 6 ай бұрын
import os filename = r'C:\Users\homeuser\Documents\de prjs\Python\manish\user_info.txt' name_list = [] try: if os.path.getsize(filename) == 0: raise Exception("Empty File") else: with open(filename,'r') as handler: # read next line next(handler) # rstrip to remove '/n at end of line, split at , and name is at position 1 for line in handler: name_element = line.rstrip().split(',')[1] if not(name_element.startswith('"') and name_element.endswith('"')): raise ValueError(f"Name is not properly quoted : {name_element}") name_list.append(name_element) print(set(name_list)) handler.close() except FileNotFoundError: print("File cannot be opened: ", filename) except ValueError as e: print(e) except Exception as e: print(e)
@gazalaamin5076
@gazalaamin5076 2 ай бұрын
try: with open('file_exception_task.txt', 'r') as file: content = file.readline() print(content) values = [] unique_name = [] if not content: logger.info("The file is empty") raise Exception("Please insert data into file") else: for line in file: values = line.split(',')[1] if not(values.startswith('"') and values.endswith('"')): logger.info("The file contains non string name") raise ValueError("Please enter valid string name") unique_name.append(values[1:-1]) logger.info(unique_name) except FileNotFoundError as e: logger.info(f"The file does not exist") raise e except ValueError as e: raise e except Exception as e: raise e
configuration file handling in python | Lec-24
35:50
MANISH KUMAR
Рет қаралды 1,7 М.
AES encryption in python | Lec-25
22:58
MANISH KUMAR
Рет қаралды 1,3 М.
Lazy days…
00:24
Anwar Jibawi
Рет қаралды 8 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 86 МЛН
Quilt Challenge, No Skills, Just Luck#Funnyfamily #Partygames #Funny
00:32
Family Games Media
Рет қаралды 47 МЛН
Tredence - New office inauguration
1:40
Tredence
Рет қаралды 2,5 М.
list comprehension in python | Lec-13
15:26
MANISH KUMAR
Рет қаралды 2,4 М.
*args and **kwargs in python | Lec-22
33:21
MANISH KUMAR
Рет қаралды 1,8 М.
Lecture 76: Exception Handling in C++
1:00:37
Coder Army
Рет қаралды 39 М.
scd2 in spark | Lec-24
15:19
MANISH KUMAR
Рет қаралды 14 М.
Flatten Nested Json in PySpark
9:22
GeekCoders
Рет қаралды 5 М.