Posts By vishavjeet (38)

How can convert excel to csv in python?

 excel_raw_data = pd.read_excel(request.FILES.get('feed_product'))
# Convert excel to csv file
excel_raw_data.to_csv("feed_product.csv", index=None, header=True)
# read csv file
csv_raw_data = pd.DataFrame(pd.read_csv("feed_product.csv"))

How can count python files in given directory?

def count_py_files_in_repos(dirname):
    #os.chdir("D:/deebaco")
   # if os.path.exists(os.path.join(dirname, '.git')):
   if os.path.exists(os.path.join(dirname)):
      count = 0
     for root, dirs, files in os.walk(dirname):
        count += len([f for f in files if f.endswith('.py')])
     print(f"{dirname} has {count} python files.")

    for name in os.listdir(dirname):
       path = os.path.join(dirname, name)
       if os.path.isdir(path):
              count_py_files_in_repos(path)


count_py_files_in_repos('D:/deebaco/apipy/master')

How to make generator to produce N prime number?

def is_prime(num):

    for n in range(2, num):

        if num % n == 0:

            return False

    return True


# using Generator(Yield)

def generate_n_prime(num):

    i = 2

    while num:

        if is_prime(i):

           yield i

         num -= 1

      i += 1


it = generate_n_prime(10)

for i in it:

    print(i, end=' ')


#2 3 5 7 11 13 17 19 23 29

List Of All Python Exception Classes

The class hierarchy for built-in exceptions is:

BaseException

 +-- SystemExit

 +-- KeyboardInterrupt

 +-- GeneratorExit

 +-- Exception( All given below Exception class are childs of Exception Class)

    +-- StopIteration

    +-- StopAsyncIteration

    +-- ArithmeticError

    |    +-- FloatingPointError  (Not currently used.)

    |    +-- OverflowError

    |    +-- ZeroDivisionError

   +-- AssertionError

   +-- AttributeError

  +-- BufferError

  +-- EOFError

  +-- ImportError

  |    +-- ModuleNotFoundError

  +-- LookupError

  |    +-- IndexError

  |    +-- KeyError

  +-- MemoryError

  +-- NameError

  |    +-- UnboundLocalError

  +-- OSError

  |    +-- BlockingIOError

  |    +-- ChildProcessError

  |    +-- ConnectionError

  |    |     +-- BrokenPipeError

  |    |     +-- ConnectionAbortedError

  |    |     +-- ConnectionRefusedError

  |    |    +-- ConnectionResetError

  |    +-- FileExistsError

  |    +-- FileNotFoundError

  |   +-- InterruptedError

  |   +-- IsADirectoryError

  |   +-- NotADirectoryError

  |   +-- PermissionError

  |   +-- ProcessLookupError

  |   +-- TimeoutError

 +-- ReferenceError

 +-- RuntimeError

 |    +-- NotImplementedError

 |    +-- RecursionError

 +-- SyntaxError

 |    +-- IndentationError

 |    +-- TabError

 +-- SystemError

 +-- TypeError

 +-- ValueError

  |    +-- UnicodeError

  |    +-- UnicodeDecodeError

  |    +-- UnicodeEncodeError

  |    +-- UnicodeTranslateError

   +-- Warning

     +-- DeprecationWarning

     +-- PendingDeprecationWarning

     +-- RuntimeWarning

     +-- SyntaxWarning

     +-- UserWarning

     +-- FutureWarning

     +-- ImportWarning

     +-- UnicodeWarning

     +-- BytesWarning

    +-- ResourceWarning

1 2 3 Next Last