ValueError: invalid literal for int() with base 10
To Convert a Floating value to a Number in Python or Jython use the below method
int(float(259.0))
The output would be 259
ValueError: invalid literal for int() with base 10
To Convert a Floating value to a Number in Python or Jython use the below method
int(float(259.0))
The output would be 259
Names = "A, B, C, D, E" arrNames = Names.split(",") for x in range(len(arrNames)): print arrNames[x]
Below are the code sample which shows how to carry out basic tasks in Jython.The Files should be shared as .py extension if your are using eclipse.To know abount configuring jython in eclipse follow the
Link
Printing a Message in Console
print 1 + 1 print "Hello" + " World " print "Hello Jython " print "Jython ", "Scripting"
Simple Jython Function that Takes Argument and Print Values
def displayName(x): print x displayName("Mugil")
Jython Function which Calculates the Factorial of a Number
def factorial(x): if x<=1 : return 1 return x * factorial(x-1) print factorial(3)
Function which compares Two Strings in If Condition
def compareName(FirstName, LastName): if FirstName == LastName: print "They are Equal" else: print "They are Not Equal" compareName("Jython", "Jython")
Conditional Operators
Function which Perform AND, OR and Not Ops
def BoolOps(pOne, pTwo): if pOne and pTwo: print "They are Equal" else: print "They are Not Equal" BoolOps(1,0)
Jython Supports Three Numeric Values as Below
Array from Delimited String and Looping through It
Names = "A, B, C, D, E" arrNames = Names.split(",") for x in range(len(arrNames)): print arrNames[x]
Array Function
Names = "A,B,C,D,E,F,G,H,I,J,K,L,M" arrNames = Names.split(",") #B print arrNames[1] #['B'] print arrNames[1:2] #['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'] print arrNames[1:] #['A', 'B', 'C', 'D', 'E', 'F'] print arrNames[:6] #['B', 'C', 'D', 'E'] print arrNames[1:5] #['H', 'I', 'J'] print arrNames[-6:-3] #['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'] print arrNames[:] #['A', 'C', 'E', 'G', 'I', 'K', 'M'] print arrNames[::2]
Concatenating Two Arrays together
Names1 = "A,B,C" Names2 = "D, E, F" arrNames1 = Names1.split(",") arrNames2 = Names2.split(",") arrNames = arrNames1 + arrNames2 for i in range(len(arrNames)): print arrNames[i]
import jarray import java myArray = jarray.array(["mugil", "Vannan"], java.lang.Object) rc.logMessage(str(len(myArray))) for value in myArray: rc.logMessage(value)
For Character
myArray = jarray.array(["m", "V"], "c")
For Integer
import jarray import java myArray = jarray.array([1,2,3,4,5,6], java.lang.Integer) rc.logMessage(str(len(myArray))) for value in myArray: rc.logMessage(str(value))
I am having single column in excel sheet which I want to replicate in Jython scripting and place it in adjacent column.The Content of the Column is as in the Pic below
from jxl import * from jxl.write import * from java.io import File workbook = Workbook.getWorkbook(File("$(TargetFolder)\Book2.xls")) targetWorkbook = Workbook.createWorkbook(File("$(TargetFolder)\Book2.xls"), workbook) sheet = targetWorkbook.getSheet(0) columnIndex = 0 rowCount = sheet.getRows() for i in range(rowCount): row = sheet.getRow(i) content = row[columnIndex].getContents() rc.logMessage("Content -> " + content) wc = sheet.getWritableCell(1,i) newLabel = Label(1,i, content) sheet.addCell(newLabel) targetWorkbook.write() targetWorkbook.close() workbook.close()
Before running Script
After running Script