Python

Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in other languages. Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive set of standard libraries, including NumPy, SciPy, Django, PyQuery, and PyLibrary.

Share tech news, updates, or what's on your mind.

Sign up to Post

I'm currently trying to create a script that will search for specific data in an excel document and print out a range of cells.

For example, it would search for "color" in column A. If "color" was found in A4 it would print out the data in A4, B4, C4, D4, etc.

import openpyxl
wb = openpyxl.load_workbook('example.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')

for rowOfCellObjects in sheet['A1':'C3']:
    for cellObj in rowOfCellObjects:
        print(cellObj.coordinate, cellObj.value)

Open in new window


This is the only code I have so far which is an excerpt from Al Sweigart. “Automate the Boring Stuff with Python: Practical Programming for Total Beginners.” iBooks. and the only thing it does is search for specific cell addresses and prints those.

Is this a feasible project? I'm very new to Python so I don't fully grasp its limitations.

Any help would be appreciated!
0
Want Experts Exchange at your fingertips?
LVL 9
Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

*I was tasked with writing a script (suggested in python but can be anything that the Crontab will work with) but my programming skills are definitely lacking. In the past week I have been researching and trying to learn as much as I can and below is what I came up with but I could use some help from the EE community. Thanks!

I am trying to write a script in python that will run on our Crontab of our Linux VM server. The script is supposed to recursively go through all the files on the remote server directory and download the specified file type (.csv in this case) and then promptly delete whatever files successfully download from the server. I have the following code using pysftp but I am open to using other modules such as paramiko or lftp if it's easier. It just needs to be able to be run as a Cron job in our Crontab on the linux server.

Below is my current code but it isn't work in terms of remotepath, localpath. I tried to tell it to download the file to a certain path but it's not going there. Also I don't know the command to remove the files it downloaded only. In other words files might already be on the local server and if so they should NOT download so that means the file wouldn't be removed from the remote server (if it doesnt download it shouldn't get removed).

For security purposes I have removed the server info. I did create a public/private key for ssh but unsure of how to implement it into my code.

import pysftp
import sys
import glob

srv 

Open in new window

0
I've recently decided to try and develop a script that will allow me to search for a specific word or value in a row and copy all of the data in the corresponding column, but it's proven to be too much for myself as I'm very new to Python.

If I could figure out how to search for cells within excel using Python I could go from there, but I haven't found any good resources to teach myself that skill.

Any help would be greatly appreciated.
0
hi All,

kindly help me to write a shell or python script to delete the weblogic files. The files are rotated but need to be deleted which are 30 days old. the find command doesn't delete the files because the number of files are huge.

below is the  .logs format of the weblogic servers
/wls1034/Middleware/user_projects/domains/Domain/servers/server3/logs/

server3.log00585
 server3.log00586
 server3.log00587
server3.log00588
server3.log00589
server3.log00590
server3.log00603
server3.log00604

server3.out00035
server3.out00034
server3.out00036
server3.out00037
0
Hello All,

I am new to python and coding overall. I am trying to make sense out of a particular line in the code below. This code will be used to see the files in my recycle bin. In  the second line, what are "curr, dirs, files"? are those fields, arguments of os.walk() function? I have noticed that if I change for instance "curr" for any other name, and change that parameter in the "path" line, the program still works. I am actually a little confused.

import os
rootrec = "C:\\$Recycle.Bin"
for curr, dirs, files in os.walk():
    for f in files:
        path = "%s/%s" % (curr,f)
        print(path)

Any clarification will be helpful!
0
I am looking to replace the value between two spaces in a string with a backslash. Can someone assist with the syntax.

E.G. I want

RED 123456789 White

to become

RED\White

Thanks
0
Hello all,

I am new to Python and I have a couple of questions regarding the code below. I am currently going through a lesson that shows you how to replay packets using scapy. In my try block on the code below, what does "frame[Ether].src" and "frame[IP].src" where did I get these variables from?
Is ".src" a module? what is "[Ether]" and [IP]?

I hope you guys can clarify.

Thank you!

from scapy.all import *
from scapy.utils import rdpcap

src_mac = "08:00:27:ae:6f:d7"
dst_mac = "08:00:27:87:57:be"
src_ip = "192.168.1.118"
dst_ip = "192.168.1.117"

frames = rdpcap("/root/pcaps/ping_traffic.pcap")
for frame in frames:
    try:
        frame[Ether].src = src_mac
        frame[Ether].dst = dst_mac

        if IP in frame:
            frame[IP].src = src_ip
            frame[IP].dst = dst_ip

        sendp(frame)
    except Exception as e:
        print(e)
0
I have a python script that will construct a list of futures contracts for a specified symbol, exchange, and year window.  The script will download pricing information from Quandl for each contract in the strip.  This script works properly.

import pandas as pd
from pandas import ExcelWriter
import quandl

symbol = 'ES'
exchange = 'CME/'

start_year = 2015
end_year = 2016


#Constructs a list of futures contract codes for a particular symbol
#and timeframe.
futures = []
months = 'HMUZ'
for y in range(start_year, end_year+1):
    for m in months:
        futures.append("%s%s%s%s" % (exchange, symbol, m, y))

#Download the data from Quandl.
for f in futures:
    quandl.ApiConfig.api_key = "xxxx"  
    data = quandl.get(str(f))
    data = data.drop(data.columns[[3, 4, 7]], axis=1)

I have another python script that will create a continuous futures contract from 2 consecutive contracts in the futures strip.  It uses the perpetual method; identifying the roll date as the first day where volume of the far contract exceeds volume of near contract, and then adjusts the prices of the previous 4 sessions to roll date by weighting the near and far contracts in increments/decrements of 20%.  This script works properly.

import pandas as pd
from pandas import ExcelWriter
import quandl

#Get quandl futures data
quandl.ApiConfig.api_key = "xxxx"    
contract_near = quandl.get("CME/ESH2015")
contract_far = quandl.get("CME/ESM2015")

#Drop the columns …
0
I am new to python, Now am trying to update a cell with hyperlink which refer to another cell sheet. i have tryed with below code but while click the cell its showing error with "Cannot open the specified file". Could you any one can help on this ?

from win32com.client import Dispatch
excel = Dispatch('Excel.Application')
book = excel.Workbooks.Open("Location.xls")
sheet = book.Worksheets(1)
cell_location = sheet.Cells(2,2)
cell_destination = book.Worksheets(2).Cells(1,3)
sheet.Hyperlinks.Add(Anchor=cell_location, Address=cell_destination)
book.Save()
0
I have a list (named bob) and I want to reverse the order of the list without reversing the whole list.  So, for example my bob list would be [Bob, Mom, Dad, Kelsey] and I want to reverse it to read [boB, moM, daD, yesleK] instead of [yesleK, daD, moM, boB].  

I know I have to iterate through each line and add the extracted line in from of the accumulating string, but I’m not sure how to set up the FOR loop to do so.  How would I do that?
0
[Webinar] Learn How Hackers Steal Your Credentials
LVL 9
[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

I had this question after viewing linux crontab output.

Hi everyone,
right now I am manually running this script to start a python app. I would like to do this @ reboot.

cd /home/ec2-user/console

python console.py 8090 cloud& <<this runs fine

I tried to create a small bash script ,but cant seem to make it run @ reboot time

#!/bin/bash
cd /home/ec2-user/console
python console.py 8090 cloud&

crontab -e

@reboot sh $HOME/boot_python.sh <<dosnt work

the script runs when executed manually but not @ reboot

Peter
0
Hi Experts,

 I am looking  for a data science project(using python) with complete source code and documentation , please help me with the same and will appreciate your help in this regard.  

Thanks,
SRK,
0
There are x students and y tents placed randomly on a field of dimensions 1000 by 1000 (including(0,0,)). A tent can only house up to 10 students. As a curfew is in place, all students have to find a place in the tents. I have to assign students to tents such that the number of steps walked by the students will be miNimized. Students can only move horizontally and vertically, so if their location is (0,0) and the tent's location is (10,10) then they must walk 20 steps to reach the tent.
The location of the tents will be stored in a file called tents.csv, and the location of the students stored in a file called students.csv.  
The program should read the locations of each and output the results in a file named output.csv where:
- the first row contains the number of total steps walked
- from the second row, each row contains [student ID, tent ID, steps walked, student x location, student y location, tent x location, tent y location]

The program should be able to execute any range of values as defined in the problem definition.


P/s: anyone who know how to solve please please please help me...this is the project that can decide my future......ㅠㅠㅠ
0
This code does 3 things:
1. Gets a list of ticker symbols
2. Gets pricing information from Google finance
3. Inserts the pricing information into a MySQL table

The code runs without returning any errors.  However, the MySQL table is not populated with the information.  The following is returned:
Empty DataFrame
Columns: [open_price high_price, low_price, close_price]
Index: []

I can't figure out why. Advice is needed and appreciated, thank you.

# price_retrieval.py

import datetime as dt
import MySQLdb as mdb
import pandas_datareader.data as web

# Obtain a database connection to the MySQL instance
db_host = 'localhost'
db_user = 'xxx'
db_pass = 'xxxxx'
db_name = 'securities_master'
conn = mdb.connect(db_host, db_user, db_pass, db_name)

#Obtains a list of the ticker symbols in the database.
cur = conn.cursor()
cur.execute("SELECT id, ticker FROM symbol")
data = cur.fetchmany(10)    

#Get daily historic data from google
start_date = dt.datetime(2005, 2, 1)
end_date = dt.datetime(2005, 2, 28)          
for d in data:
    try:
        gf_data = web.DataReader(str(d[1]), "google", start_date, end_date)
    except:
        pass
   
    # Create the time now
    now = dt.datetime.utcnow()
   
    #Create dictionary for db
    pricing_info = {
        'symbol_id': d[1],
        'price_date': now,
        'created_date': now,
        'last_updated_date': now,
        'open_price': gf_data['Open'],
        'high_price': …
0
How to save the output of show run (Router) in text file on desktop using python.
I have a router i have to save its config file in desktop in text format using python
0
Im trying to save the output of show run to my desktop but its not getting executed.. pls help

import paramiko

client=paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
com="ls ~/desktop"
client.connect('192.168.1.12', username='cisco', password='cisco')
output=""
stdin, stdout, stderr = client.exec_command('show run')

print "ssh succuessful. Closing connection"
stdout=stdout.readlines()
client.close()
print "Connection closed"

print stdout
print com
for line in stdout:
    output=output+line
if output!="":
    print output
else:
    print "There was no output for this command"

Open in new window

0
I'm in the Business Intelligence Department, but practically speaking we're the Reporting Department, your basic operational type of reports - lists, lists, and more lists.

I'm at an institution of higher learning, and a new project has come up for the Math Department. They want to know relationships between courses, grades, etc.

Examples:

- if someone gets a D in Calc I, what's the likelihood of graduation?  with various permutations, like taking Calc I again
- what's the likelihood of someone getting a D in Calc I, getting a D or F in Calc II
- for placing incoming students in Pre-Calc or Calc I, what are the factors that indicate success? such as Verbal SAT

So I think I've targeted the right discipline (Analytics), but not sure where to take this project.
1
Is there a way to export/extract all the values/items from a drop-down field in a PDF?

I have a PDF form, and it contains a few drop-down boxes that I can select.  I want to be able to export/extract all the values contained inside this drop-down.  Is that possible?
0
Hello, Experts,

I have an issue with a script where I want the end-user once the app is hit to see the main menu. I have four definitions doing separate tasks (updating, querying or adding in records in a database).

I want my main screen to pop up a list of items for the end users to select. I read that a def main() function is the route to go. Tested it out and I have no luck running it.

def main():
    user_exit =  5
    while True:
            #prints statements that lead the end-user to decide what they would like to do
        print('Welcome to the ROWriter Database applet! What would you like to do?')
        print('')
        print('1) Insert a new store CIM info...')
        print('2) Update an existing store CIM info...')
        print('3) Delete an exisiting store CIM info...')
        print('4) Search for CIM')
        print('5) Exit the application...')
    
            #accepts user input and converts it to a string and stores into a variable
        user_input = int(input('Enter option:'))
        #if user input is equal to the exit variable then the application exits
        if user_input == user_exit:
            break
        elif user_input == 1:
                option_one()
        elif user_input == 2:
                option_two()
        elif user_input == 4:
                option_four()
  
if__name__ == "__main__":
    
    main()  

Open in new window

0
Enroll in July's Course of the Month
LVL 9
Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Hi
i need suggestions some material for beginners to learn python...any books video lectures etc.  will be helpful...
I am asking this for my friend who is beginning to learn it and has no prior knowledge of programming..he is from a different field.

thanks
0
Hello, Experts,

Running into an issue with my python script erroring out when grabbing input from end-user and passing it to the SQL update command then executing.

Can you help me figure out what is wrong?

CODE:
def option_two():
    getCIM = int(input('Enter a store CIM number you want to edit:'))
    print('')
    print('You entered %s' % getCIM)
    print('')
    getstore_UIN = input('Enter the UIN you want to update for the the CIM you entered:')
    print('')
    print('You have successfully updated the store information database for')
    #create connection ODBC driver
    cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER={test};DATABASE={ROWStage};UID={test};PWD={test123}')

    cursor = cnxn.cursor()

    #variables that grab user input variables and passes them to SQL command
    store_UIN = getstore_UIN
    store_CIM = getCIM

    SQLCommand = ("UPDATE StoreInfo SET store_UIN = ? WHERE store_CIM = ?", store_UIN, store_CIM)
    #store_UIN, store_Num, store_FileName, store_Name, store_CIM, store_OpeningDate, store_Email, store_Bays, store_Operator, 
    #store_Phone, store_Market, franchisee_Email, store_Street, store_City, store_State, store_Zip
    #("UPDATE progress SET CockpitDrill = ? WHERE progress_primarykey = ?", newcockpitdrillvalue, oldprimarykeyvalue)

    #stores getcim variable into a readable variable list
    cursor.execute(SQLCommand, [store_UIN])
     
    #commit the sql command
    cnxn.commit()

    input('Press enter to exit program')

Open in new window


ERROR:

error.jpg
0
How to automate a cisco ios upgrade process using a script like python or any other script or any other tool
0
I run an order import routine and I am modify a python script to check that they are in the system once it is done importing.  My code lookeds like this.  I am trying to learn the pieces of putting this together.  If I take the date part out I can get it to return orders to the screen so I know it is a formatting issue on the date that is making this not work correctly. I am using the pyodbc library.

d = datetime.datetime(2017, 5, 3)
print (d)
sql = "select OrderNum, POnum, OrderDate from PUB.OrderHed where OrderDate ="+d
cnxn = pyodbc.connect("DRIVER={Progress OpenEdge 10.1b Driver};HostName="+host+";DATABASENAME="+database+";PORTNUMBER="+port+";LogonID="+login+";PASSWORD="+password)
cursor = cnxn.cursor()
cursor.execute(sql)
rows = cursor.fetchall()
for row in rows:
    print(row.OrderNum, row.PONum, row.OrderDate)

this is what it returns.

    sql = "select OrderNum, POnum, OrderDate from PUB.OrderHed where OrderDate ="+d

TypeError: must be str, not datetime.datetime

I have tried everything I can find to convert to a string but then I am not sure that if it is a string format trying to pull a datetime format from progress if that is the error.

Any help would be greatly apppreciated.
0
Hi Team,

I am having a Python script which insert data into PostgreSQL from XML file. However, we have updated the python version to 3.6.1 it does not add any data into PostgreSQL database and does not throw any error message on command line screen.
Can you please suggest what's goes wrong in the below code...
#/usr/bin/python3.6.1

# import modules
import sys
import psycopg2
import datetime


#import cursors
now = datetime.datetime.now()
# current data and time
dt = now.strftime("%Y%m%dT%H%M%S")

# xml tree access
#from xml.etree import ElementTree
import xml.etree.ElementTree as ET

# incremental variable
x = 0
with open(sys.argv[1], 'rt',encoding="utf8") as f:
    	#tree = ElementTree.parse(f)
		tree = ET.parse(f)
# connection to postgreSQL database
try:
    conn=psycopg2.connect("dbname='####' user='postgres' password='XXXX'")
except:
    print ("Hey I am unable to connect to the database.")
cur = conn.cursor()
# access the xml tree element nodes
try:
	for node in tree.findall('.//tu'):
		src = node.find('tuv[1]/seg')
		tgt = node.find('tuv[2]/seg')
		
		source = src.text.encode("utf-8")
		target = tgt.text.encode("utf-8")
		
		#query to database table
		query = "INSERT INTO MT_DB (SRC_LOCALE,SRC_DATA,TGT_LOCALE,TGT_DATA,DOMAIN,DATETIME,TYPE) VALUES (%s, %s, %s, %s, %s, %s, %s)"
		data = (sys.argv[2],source,sys.argv[3],target,sys.argv[4],str(dt),sys.argv[5])

		#for locale swap with data
		#data = 

Open in new window

0
I have an ssh client called "termius" to import all my hosts from a notepad. I am using a script with python and github to make all this work. I dont know much about scripting at all but I've been provided with all these tools to make this work. Does anyone know what the error below mean? As you can see the import was successful but it errors out later. I'm using a siple notepad file. I provided a snip it of how it looks.

notepad:
Abilene North Place,10.50.72.254
Abilene South Place,10.50.65.254
Abilene west Place,10.50.65.251
Addison Place,10.50.27.254
test 500,5.5.5.5

a.png
0

Python

Python is a widely used general-purpose, high-level programming language. Its design philosophy emphasizes code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in other languages. Python supports multiple programming paradigms, including object-oriented, imperative and functional programming or procedural styles. It features a dynamic type system and automatic memory management and has a large and comprehensive set of standard libraries, including NumPy, SciPy, Django, PyQuery, and PyLibrary.