Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Friday, September 2, 2022

py0024 Python error, while reading CSV file

 Python error, while reading CSV file

import pandas as pd
f = 'C:\\Users\\*****\\Desktop\\Emp.csv'
from datetime import datetime
custom_date_parser = lambda x: datetime.strptime(x,"%d-%m-%Y %H:%M:%S")
df = pd.read_csv(f,nrows=100,parse_dates=True,date_parser=custom_date_parser)
df.info()



During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers\readers.py", line 586, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers\readers.py", line 488, in _read
    return parser.read(nrows)
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers\readers.py", line 1047, in read
    index, columns, col_dict = self._engine.read(nrows)
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 309, in read
    names, data = self._do_date_conversions(names, data)
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers\base_parser.py", line 802, in _do_date_conversions
    keep_date_col=self.keep_date_col,
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers\base_parser.py", line 1093, in _process_date_conversion
    data_dict[colspec] = converter(data_dict[colspec])
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\parsers\base_parser.py", line 1055, in converter
    return generic_parser(date_parser, *date_cols)
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\date_converters.py", line 100, in generic_parser
    results[i] = parse_func(*args)
  File "<stdin>", line 1, in <lambda>
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37-32\lib\_strptime.py", line 588, in _strptime_datetime
    return cls(*args)
ValueError: second must be in 0..59
>>> df.info()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'df' is not defined




second (in date data) should be 3 digit in file, in my case it was 05:33:09345 five digit data. having bad data in csv

Solution
As error clear said, Date format in file doesnt match with the Date pass in code. Seconds of date in CSV file should be of 3 digits only. Verify your data in CSV and try to resolve data in CSV file.

Thursday, February 18, 2021

py0023 ValueError: not enough values to unpack

I am following "Learn Python 3 the Hard Way_ A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code ( PDFDrive ).pdf" document to to read out basic concept of python.

At excercide 13 "ex13.py", tell how to pass variable in python script while executing from cmd.

However i am doing practie @pycharm, faced below error while excuting script.

Content in file 
from sys import argv
script, first, second, third = argv
print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
print("Your third variable is:", third)

Error while execution

C:\Users\******\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/******/PycharmProjects/learnPython/ex13.py
Traceback (most recent call last):
  File "C:/Users/******/PycharmProjects/learnPython/ex13.py", line 3, in <module>
    script, first, second, third = argv
ValueError: not enough values to unpack (expected 4, got 1)
Process finished with exit code 1

So conclusion is as i told i was executing script in pycharm, which was wrong approach.

Microsoft Windows [Version *********
(c) 2018 Microsoft Corporation. All rights reserved.
C:\Users\******>cd C:\Users\*****\AppData\Local\Programs\Python\Python37-32\
C:\Users\******\AppData\Local\Programs\Python\Python37-32>python.exe C:/Users/******/PycharmProjects/learnPython/ex13.py 1st 2nd 3rd
The script is called: C:/Users/******/PycharmProjects/learnPython/test.py
Your first variable is: 1st
Your second variable is: 2nd
Your third variable is: 3rd

1st, 2nd, 3rd is variable value we passed in python script during execution

Monday, December 28, 2020

py0022 : 'python' is not recognized as an internal or external command,

C:\Users\******\AppData\Local\Programs\Python\Python37-32\Scripts>python --version

'python' is not recognized as an internal or external command,

operable program or batch file.


Above error doesnt mean Python is not installed on your system. Python may or may not installed on your system. You can check this manually. Go to C drive search for python folder.


Default installation can be under path "C:\Users\******\AppData\Local\Programs\Python"


Foe me this command worked under path 


C:\Users\*****\AppData\Local\Programs\Python\Python37-32>python --version

Python 3.7.2

Tuesday, December 22, 2020

py0021 : Make Python connection with SQLServer database

Below is the code which can be used for to make connection with SQLServer database. Make sure python have already imported with pyodbc driver. If you have missing driver, use below command to install driver.

>pip install pyodbc

If you faced below message in output that means driver already installed
"Requirement already satisfied: pyodbc in c:\users\******\appdata\local\programs\python\python37-32\lib\site-packages (4.0.25)"

Code : 
import pyodbc
conn = pyodbc.connect('Driver={SQL Server};'
                      'Server=IPAddress;'
                      'Database=databaseName;'
                      'username=sa;'
                      'password=dbpASSWORD;'
                      )
cursor = conn.cursor()
cursor.execute('SELECT * FROM information_schema.tables')
for row in cursor:
print(row)

py0020 : Python PIP install Error

I was trying to install pyodbc driver for sqlserver, but faced below error .

C:\Users\******\AppData\Local\Programs\Python\Python37-32\Scripts>pip install pyodbc
Requirement already satisfied: pyodbc in c:\users\******\appdata\local\programs\python\python37-32\lib\site-packages (4.0.25)
You are using pip version 18.1, however version 20.3.3 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

C:\Users\******\AppData\Local\Programs\Python\Python37-32\Scripts>python -m pip install --upgrade pip
'python' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\******\AppData\Local\Programs\Python\Python37-32\Scripts>cd ..

C:\Users\******\AppData\Local\Programs\Python\Python37-32>python -m pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/54/eb/4a3642e971f404d69d4f6fa3885559d67562801b99d7592487f1ecc4e017/pip-20.3.3-py2.py3-none-any.whl (1.5MB)
    100% |████████████████████████████████| 1.5MB 1.5MB/s
Installing collected packages: pip
  Found existing installation: pip 18.1
    Uninstalling pip-18.1:
      Successfully uninstalled pip-18.1
Successfully installed pip-20.3.3

C:\Users\******\AppData\Local\Programs\Python\Python37-32>

Solution : You can upgrade PIP version, as command automatically suggested by the system.

It can be upgrade by using command "python -m pip install --upgrade pip", you might face error while installating this. Only need to take care we need to change the path on CMD where python.exe exists on file system

Tuesday, June 18, 2019

py0019 : Color code in idel Python Shell

When you write a code in IDLE python shell, you may observe 4 different colors, yes these colors have some meaning:


  • Purple: It shoe that you have typed a command in python shell
  • Green: Some content sent to command
  • Blue: Any output from command will be BLUE in color
  • Black: not a command


What are commands: Print/quit are commands in python. Command generally written with round parenthesis i.e. print(), quit()

Tuesday, June 4, 2019

py0018 : Change color of button on hover and borderless button

while working in python we might need to create border less button or change the color of button on hover.
for this we can add parameter in method (def) of button, activebackground='green', borderwidth=0. Activebackground will be responsible of change color of button when mouse cursor hover on the the button
and borderWidth will remover the border from the button

import tkinter as tk

class HoverButton(tk.Button):
    def __init__(self, master, **kw):
        tk.Button.__init__(self,master=master,**kw)
        self.defaultBackground = self["background"]
        self.bind("Add**less**than**Enter**Add**greater**than", self.on_enter)
        self.bind("Add**less**than**Leave**Add**greater**than", self.on_leave)

    def on_enter(self, e):
        self['background'] = self['activebackground']

    def on_leave(self, e):
        self['background'] = self.defaultBackground

root = tk.Tk()

classButton = HoverButton(root, text="Classy Button", activebackground='green', borderwidth=0)
classButton.grid()

root.mainloop()

Note* Add**less**than** *** **Add**greater**than
please modify the code before execution, add less than and greater than sign

Monday, June 3, 2019

py0017 : Parameter 'bg' value is not used less... (ctrl+F1)

Error:
Parameter 'bg' value is not used less... (ctrl+F1)
Inspection infoL This inspection highlights local variablem parapmeters or local function unused in scope

Requirement:
How to set default color of button default to white

Solution:
Used "bg" paramter to set color value

   def numrcbtn(self, val, write=True, width=12, height=4, padx=1, pady=1) :
   return HoverButton(root, text=val, command=lambda: self.click(val, write), width=width, height=height, padx=padx, pady=pady, activebackground='Gainsboro' , bg='white')
   
   

Wednesday, March 13, 2019

py0016 : ModuleNotFoundError: No module named 'Tkinter'

Traceback (most recent call last):
  File "C:/Users/*****/PycharmProjects/Cal1/_main_.py", line 2, in
    from Tkinter import *
ModuleNotFoundError: No module named 'Tkinter'

While import from tkinter i was getting error as "ModuleNotFoundError: No module named 'Tkinter'". As further investigation i found tkinter is the part of python software that we have installed in actual. So no need to run anyother
pip install command for "tkinter".

i have recheck my code as i written "from Tkinter import *", so the error was here i used "T" capital T instead of "t"

so if you get the error i advise check your spelling before doing anything it should all in lowercase

Friday, February 22, 2019

py0015: NameError: name 'Select' is not defined

Error 
select = Select(driver.find_element_by_id('id****')) 
NameError: name 'Select' is not defined

This error i received when i tried to select value from drop down menu while doing automation in python selenium

How ever i came to know that i was using correct code but some import class is missing from header of file. I need to add "from selenium.webdriver.support.select import Select" in main file or same file so that code will work fine.

Solution:
from selenium.webdriver.support.select import Select

Correct code for click on drop down:
select = Select(driver.find_element_by_id('**id**'))
select.select_by_visible_text('String')

Thursday, February 21, 2019

py0014: table_name1 = table_name.replace(',','') AttributeError: 'pyodbc.Row' object has no attribute 'replace'

Problem :
below is my sample code, where i am getting error table_name1 = table_name.replace(',','') AttributeError: 'pyodbc.Row' object has no attribute 'replace' and in same example you can cover how to use replace in python
    cursor.execute('select table_name from information_schema.tables')
    for table_name in cursor:
        table_name1 = table_name.replace(',','')
        print (table_name1)

I was trying to loop the list of all table names which are in schema using query "select table_name from information_schema.tables". The intention was to do some action based on the table exists or not.

Why i used replace:
suppose after for loop when i use command "print (table_name)", it gives output enclosed in single quotes and postfix by comma, so i used replace function to replace comma and it starts throwing above error.

Solution:
if you notice code above i gave "table_name" in for loop and for replace function we have to write it as str.replace(old,new). But in my case replace was not able to get any string in "table_name" because "table_name" is just act as "i" here in for loop and it is not a attribute in this situation.

So i have to use "table_name.table_name.replace(',','')"

Correct code as below:
    cursor.execute('select table_name from intermediate_table')
    for tb in cursor:

        table_name1 = tb.table_name.replace(',','')
        print (table_name1)

Here i have replace table_name in for loop with "tb" for better visibilty and better understanding.

Monday, February 18, 2019

py0013 : use function from another file in main file python

To make code convient and readable form it is neccessary to break code in multiple files, where you can write your code and write function can be later call on any where in the project. In python its easy to do.

Below is the sample code, i am taking the example from my previous post

py0012 : try catch in Python/ DB connection in try catch

which is having file name "file1" and t funcation made which will return something on db connection pass or fail and that can be used in mail file as per below code example. First we need to import file, then use funcation name directly "t()" without any file reference.


from file1 import *
if t() == "DBConnectionError":
    # your code will stop here, dependent file will not throw any error
exit(0)


if t() == 1:
# if database connection pass it will move further as per the written commands, this also show that how we can handle error on other files call
    driver = selenium.webdriver.Firefox(executable_path=FirefoxDriver, firefox_binary=binary, firefox_profile=profile)

py0012 : try catch in Python / DB connection in try catch

try catch is preliminary to add in your code, other wise if it break in between it looks ugly when error come to end user. To make code clean and show user friendly error you can use try catch, see one of the example below when database connection.

Earlier when database connection fails, it show red color unexpected error and since this file is being in use further so need to use try catch which will return something so that further can be used as if else for any type of condition

import os
import pyodbc
from configuration import *

try:
    crsrconn = pyodbc .connect("Driver={SQL Server Native Client 11.0};"
                                "Server="+ServerName+";"
                                "Database="+DatabaseName+";"
                                "Trusted_Connection=yes;"
                                "pwd="+Password+";")
    cursor = crsrconn.cursor()
    cursor.execute('SELECT count(*) rn FROM *****')
    for row in cursor:

        if row != 0:
            def t(): return 1
            print ('1')
        #else:
         #   def t(): return 0
          #  print ('0')

except pyodbc.Error as err:
    print("DBConnectionError")
    def t(): return ("DBConnectionError")

py0011 : Learnt Something impressive in Python

Code writing techniques

Today i tried to write some code in python like i used for , if , else, try, catch, return. By using this came to know python has some limitation, no we cant say limitation its syntax.


  • For "if" we can not keep the code below on same starting column point, you have to give "tab" space in front of them for any hierarchy .
  • write "return" like this, def t() return 1 or def t() return "true"

Thursday, February 14, 2019

py0010 : Firefox not opening Websites which are in Tunnel/Proxy

If your system has implemented with proxy or tunnel and you want to open a web link (which are in tunnel or in proxy)
note* generally these kind of websites not worked on internet, but the firefox leaunched by Selenium/python using Geckodriver is open with internet without any proxy setting given in your system. So you have add some lines of code in your .py file before Geckodriverr called so that it will open firefox with system proxy settings.

#Add proxy IP Address
proxy = "***.***.***.***"
#Add proxy port
port = int("****")

cap = DesiredCapabilities().FIREFOX
cap["marionette"] = True

profile = webdriver.FirefoxProfile()
profile.set_preference("network.proxy.type", 1)
profile.set_preference("network.proxy.http", proxy)
profile.set_preference("network.proxy.http_port", port)
profile.set_preference("network.proxy.ssl", proxy)
profile.set_preference("network.proxy.ssl_port", port)
profile.update_preferences()

Related/Helpful Links:
py0005 : selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities
py0004 : Begginner error, Open Firefox

py0009 : How to Declare Variable and Use variable in same file of Other file

Assume that i have two files in my project first is "__init__.py" another one is "configuration.py" and i am using config file for different type of parameters which needs to change timely. The case is declared variable in config file i have to access them in same file or different file in all project.
See below how this can be done.

Configuration.py :
FirefoxPath = "C:/Users/*****/AppData/Local/Mozilla Firefox/firefox.exe"
FirefoxDriver = "C:\\Users\\*****\\Desktop\\python\\geckodriver-v0.24.0-win64\\geckodriver.exe"


__init__.py :
import os
from Configuration import *
binary = FirefoxBinary(FirefoxPath)
driver = selenium.webdriver.Firefox(executable_path=FirefoxDriver, firefox_binary=binary, firefox_profile=profile)

Tuesday, February 12, 2019

py0008 : connection with sqlserver database

First of all you need to install pyodbc package if not you might face this error "unknown command pyodbc"

Open cmd and use below command to install :
pip install pyodbc

Helpfull Link :

py0003 : pip install selenium SyntaxError: invalid syntax, while install selenium


install above package. for me it is very easy step, to install this package connect with database without any error in single shot.

I used below code to connect with database.

import pyodbc
crsrconnection = pyodbc .connect("Driver={SQL Server Native Client 11.0};"
                      "Server=IPAddress Or Server Name\Instance;"
                      "Database=****;"
                      "Trusted_Connection=yes;pwd=****")


cursor = crsrconnection.cursor()
cursor.execute('SELECT count(*) rn FROM sametable')

for row in cursor:
    print(row)

py0007 : Click on div ul li span class "Display Name" Selenium Python

Being a startup its difficult to work around, but struggle of 2 days i was able to click on element having flow like div/ul/li/span/class.

Searched lot on google most of examples are with ID, but in my case id was not defined so i tried many time with class name and Display name with possible probability. At last able to Click.

Solution:
Add below lines in your code
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as wait

wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='DisplayName']"))).click()



Wednesday, February 6, 2019

py0006 : selenium.common.exceptions.WebDriverException: Message: Failed to find firefox binary. You can set it by specifying the path to 'firefox_binary':

I have reinstalled the Firefox earlier it was working fine, suddenly it started giving this error. If you read error carefully it is going to search firefox.exe (binary file) which is unable to find.

Locate your firefox.exe file and set in a variable and add below lines in your code

from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
binary = FirefoxBinary('C:/Users/****/AppData/Local/Mozilla Firefox/firefox.exe')
driver = selenium.webdriver.Firefox( firefox_binary=binary)

py0005 : selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities

Error : selenium.common.exceptions.SessionNotCreatedException: Message: Unable to find a matching set of capabilities

I am using latest Selenium 3.8.0 and GeckoDriver and Firefox version 64.0.2.
Below Firefox version 64.0.2 you might have set capability marionette to False using DesiredCapabilities

Solution
Add below code
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
cap = DesiredCapabilities().FIREFOX
cap["marionette"] = False

web stats