How to paste individual fields from external list

Asked by Keith Grimm

I'm attempting to automate some data entry. I have a list with a few hundred lines and 2 fields per line. I need to be able to copy/paste the fields from each line separately into my business software. There is also some static information being entered into the business software that will be the same for every line.

Currently Sikuli is configured to open Excel, copy information (6 digit number) from a starting point cell and switch to the business software, paste the number, then tab to several fields and enter the static information using keyboard commands. It then switches back to Excel, tabs to the next field within the same line and copies the information. Then switch again to the business software and the field 2 info is pasted into the software. After a few more keyboard shortcuts the business software goes back to it's original state and the process begins again, but with the fields from line 2. This goes on until all lines are entered. Switching back and forth between the applications is slower than I prefer.

How can I import the data from a .csv/text/.xls file without using Excel as a go-between? What is the syntax to paste/type the appropriate fields?

Any explanation is appreciated as I've no programming experience.

I'm using Windows XP Pro and Office 2003.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

The principle would be, to use some python basics to read the file into a list of lists (which would be your lines).

Then loop thru this list and paste each entry where it should be.

If you manage to produce your data-entry-file as a CSV ("field1", "field2", ...), you could minimize your coding.

Before putting effort into converting your data, just try your logic with a handwritten list of lists to simulate your file input
myFile=[["field1", "field2", ...], ["field1", "field2", ...], ...]
for line in myFile:
   f1 = line[0]
   f2 = line[1]
   ...
   # here goes your processing for each line

if this works, get your data from a file:
myF = open("path-to-csv-file")
myFile = []
for line in myF.readlines():
   myFile += eval("[" + line + "]")

hope it helps. your welcome to come back. pls. paste your script if you need more help.

Revision history for this message
Keith Grimm (keithg-superdogpetfood) said :
#2

Thanks for the help. I tried to simulate the file input based on what you posted. I get a syntax error and don't know why.
I wasn't able to test the simulation list because I couldn't get past the error.
The error states:

[sikuli] [Error] Traceback (innermost last):
  (no code object) at line 0
SyntaxError: ('invalid syntax', ('C:\\DOCUME~1\\keithg\\LOCALS~1\\Temp\\sikuli-tmp1974902090812225719.py', 1, 30, 'myFile=[["field1", "field2", ...], ["field1", "field2", ...], ...]'))

my script with simulation info:

myFile=[["field1", "field2", ...], ["field1", "field2", ...], ...]
for line in myFile:
   f1 = line[0]
   f2 = line[1]
   ...
   switchApp("apprise")# apprise is the business software in use
   sleep(1)
   paste(f1)# pastes field1 into text box
   sleep(1) # pauses to give apprise time to find item number from field1
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type("ALL")# defines pricing region
   type(Key.TAB)# skips to the next field
   type("w", KEY_ALT)# keyboard shortcut to begin new definition in apprise
   sleep(1)
   type("04012010")# start date
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type("04302010")# end date
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type("=")# keyboard shortcut to toggle check box to override margin check
   type(Key.TAB)# skips to the next field
   sleep(1)
   type("b")# enters promotional tracking code
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   type(Key.TAB)# skips to the next field
   sleep(1)
   paste(f2)# pastes field2 into text box in apprise
   sleep(1)
   type("k", KEY_ALT)# keyboard shortcut to create new definition
   sleep(1)
   type(Key.ENTER)# hits ok key after successful completion of new definition
   sleep(1)
   type(Key.TAB)# resets apprise to begin next line
   sleep(1)

Revision history for this message
Best RaiMan (raimund-hocke) said :
#3

Sorry, for misleading:
The 3 points were a sign for you, that you could have more than 2 fields ;-)

Here is your script, as it should do what you want.

Since you need some TAB's in between, I made a def() (function) that when called, does the wanted number of type-TAB.

def myTab(cnt):
   for i in range(1,cnt+1):
      type(Key.TAB)
myFile=[["field1", "field2"], ["field1", "field2"]]
for line in myFile:
   f1 = line[0]
   f2 = line[1]
   switchApp("Safari")# apprise is the business software in use
   sleep(1)
   paste(f1)# pastes field1 into text box
   sleep(1) # pauses to give apprise time to find item number from field
   myTab(4)
   type("ALL")# defines pricing region
   myTab(1)
   type("w", KEY_ALT)# keyboard shortcut to begin new definition in apprise
   sleep(1)
   type("04012010")# start date
   myTab(2)
   type("04302010")# end date
   myTab(6)
   type("=")# keyboard shortcut to toggle check box to override margin check
   myTab(1)
   sleep(1)
   type("b")# enters promotional tracking code
   myTab(6)
   sleep(1)
   paste(f2)# pastes field2 into text box in apprise
   sleep(1)
   type("k", KEY_ALT)# keyboard shortcut to create new definition
   sleep(1)
   type(Key.ENTER)# hits ok key after successful completion of new definition
   sleep(1)
   myTab(1)
   sleep(1)

if you need more help, you are welcome. just use my mailadress and attach a textfile with your script or paste it into the mail directly. So we can communicate by mail directly.
In the consequence you should set this question to solved with an applicable comment.

Revision history for this message
RaiMan (raimund-hocke) said :
#4

Sorry, in the switchApp statement reset Safari to your app - it was ma test :-)

Revision history for this message
Keith Grimm (keithg-superdogpetfood) said :
#5

Thanks RaiMan, that solved my question.

Revision history for this message
alcia burke (alcianicole) said :
#6

Hi

I am trying to use this method to get data from an external file
myF = open("path-to-csv-file")
myFile = []
for line in myF.readlines():
   myFile += eval("[" + line + "]")

However, I am getting the following error:

AttributeError: 'list' object has no attribute 'readlines'

Revision history for this message
RaiMan (raimund-hocke) said :
#7

I just tried it:
if the file "path-to-csv-file" exists, it runs perfectly with me.

start with

myF = open("path-to-csv-file")
for line in myF.readlines():
   print line

to see, wether your file is read correctly.

Additionally, I think you have to say
   myFile += eval("[" + line.strip() + "]")
to get rid of the newline at the end of a line.

If you need more help, feel free to contact me directly with my e-mail in my personal launchpad entry. then you could use attachements.

Revision history for this message
Robert (r-mlinaric) said :
#8

Hi there Rai man!

I'm interested in something like this. I need to open csv file with certain number of Ip addresses and copy one at the time and paste this IP address into appropriate field at logon window to launch certain application. After I run sikuli script to get needed data from this application, I quit this application with sikuli command and the sikuli script soes the loop to again start same script to open logon window to paste IP address to open this application but this time for different element, and this time with new ip address copied from csv file.

Can You fill me in how to do the copying and pasting and looping?

Revision history for this message
RaiMan (raimund-hocke) said :
#9

your case sounds interesting.

I'm willing to help you getting on the road.

It is much easier, to communicate directly using mail. So if you agree, send me a mail using the address in may personal launchpad entry https://launchpad.net/~raimund-hocke.

helpful as attachement would be:
- an example of your csv
- a screenshot of your login situation
- a rough layout of your workflow

Revision history for this message
alcia burke (alcianicole) said :
#10

Hi,

In regard to my scrip Failing, here is the code. I also want to be able to
run through a form and test different values. For example, see url:
https://subs.timeinc.net/SI/si_cntrl0409.jhtml?experience_id=231435&source_id=18&_requestid=53780&_requestid=53780

Also, see my sikuli script which is faling at line 8:

def myTab(cnt):
 for i in range(1,cnt+1):
  type(Key.TAB)

myF = open(getBundlePath()+"//test_data.csv")
myLines=[ ]
for line in myF.readlines():
 myLines+=[eval("["+line.strip()+"]")]
 f1 = line[0]
 f2 = line[1]
 f3 = line[2]
 f4 = line[3]
 f5 = line[4]
 f6 = line[5]
openApp("Firefox.app")
click( )
type("www.si.com\n")
click( )
sleep(2)
myTab(1)
paste(f1)
sleep(1)
myTab(1)
paste(f2)
myTab(1)
paste(f3)
myTab(2)
paste(f4)
myTab(1)
paste(f5)
myTab(1)
paste(f6)
myTab(1)
paste(f7)
myTab(1)
paste(f8)

Thanks

On Wed, May 5, 2010 at 2:36 PM, RaiMan <<email address hidden>
> wrote:

> Question #105543 on Sikuli changed:
> https://answers.launchpad.net/sikuli/+question/105543
>
> RaiMan posted a new comment:
> your case sounds interesting.
>
> I'm willing to help you getting on the road.
>
> It is much easier, to communicate directly using mail. So if you agree,
> send me a mail using the address in may personal launchpad entry
> https://launchpad.net/~raimund-hocke.
>
> helpful as attachement would be:
> - an example of your csv
> - a screenshot of your login situation
> - a rough layout of your workflow
>
> --
> You received this question notification because you are a direct
> subscriber of the question.
>

--
AlciaNicole
"Courage doesn't always roar. Sometimes courage is the quiet voice at the
end of the day saying, 'I will try again tomorrow"

Revision history for this message
Anurag (annubhav10) said :
#11

Hello guys,

I need some urgent help from you guys, Actually i need to call a data from notepad line by line according to my usage say
if txt looks like
name Anurag age 12 sex male.
name Lokesh age 24 sex male.
name Ashu age 23 sex male.
and so on
i like to call name in script, it should reflect as Anurag and age as 12 and sex as male.
for the second time it should reflect lokesh, 24 and male so on.

Please help me i really need this.

thanks in advance.

Revision history for this message
Eugene S (shragovich) said :
#12

Please post your new question separately.