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.
@sharadsonwane65947 ай бұрын
👍
@bhargavnagineni93227 ай бұрын
Hi bro.. can you please share the topics required in python for data engineer
@parthmm7 ай бұрын
sir how to practice python for data analyst
@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)
@abhijeetsinghbatra81433 ай бұрын
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)
@hardeepcharak7 ай бұрын
Is that you in DP?
@ankitdhurbey70937 ай бұрын
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-z1m4 ай бұрын
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)
@safiadawood56376 ай бұрын
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)
@gazalaamin50762 ай бұрын
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