Read contents of CSV file into list and print contents of list into web text field using pyautogui - for-loop

When I create a list from values in a CSV file, I am able to print out the contents of the list using the built in print function, but when I try to enter the elements of the list into an input box using pyautogui,only the first element of the list is printed.
Here is the code which is not working with screenshot of error attached.
Idle Error Message
import csv, pyautogui, time
file = open('serials.csv', 'r')
serial_list = list(csv.reader(file))
file.close
print(serial_list)
time.sleep(5)
i = 0
for serial in serial_list:
pyautogui.typewrite(serial_list[i][i])
i +=1
I was able to get the desired results using the Python code below which I generated using VBA, but would like to learn how to do this properly using Python.
import pyautogui, time
time.sleep(3)
pyautogui.write("2300-xxxx1")
pyautogui.press('tab')
pyautogui.write("2300-xxxx2")
pyautogui.press('tab')
pyautogui.write("2300-xxxx3")
pyautogui.press('tab')
pyautogui.write("2300-xxxx4")
pyautogui.press('tab')
pyautogui.write("2300-xxxx5")

yo, I think issue is in this line of code:
pyautogui.typewrite(serial_list[i][i])
Here, you trying to access a specific element of the 2D list serial_list using serial_list[i][i] where i is the index variable. However, this will only give you the first element of the list.
You should use the variable serial that you defined in the for loop to access the current element of the list:
pyautogui.typewrite(serial)
Also, since the serial_list is a 2D list of strings, you want to convert the elements of list to strings before passing it to the pyautogui.typewrite() function like this:
for serial in serial_list:
pyautogui.typewrite(str(serial))
Also, you should call the file.close method with paranthesis file.close() to close the file properly.
Also, you should use the pyautogui.press('tab') function after each pyautogui.typewrite() function call to move to the next input box.
So the final code will look like:
import csv, pyautogui, time
file = open('serials.csv', 'r')
serial_list = list(csv.reader(file))
file.close()
print(serial_list)
time.sleep(5)
for serial in serial_list:
pyautogui.typewrite(str(serial))
pyautogui.press('tab')
Now it will work.
why? cuz 🅱️

Related

Passing contents of CSV file to script function as arguments using Ruby

I created a CSV file, no headers, two columns per row. I.e.:
ARG001,ARG001a
ARG002,ARG002a
ARG003,ARG003a
I can read it and print out a specific row of the array:
require 'csv'
data = CSV.read("recTest.csv")
print data[0]
With my output looking like this:
["ARG001", "ARG001a"]
What I am trying to do is pass as many rows of data to a function that'll use them within the query string of a URL. I.e.:
start firefox
https://desiredSite.com/status?queryStringArg1=ARG001&querySTringArg2=ARG001a
Extremely new with Ruby, any help would be great. Thx in advance!

Changing file name of writedlm in Julia with a for loop

Is it possible to change the name of a file for each iteration of a loop in Julia, with writedlm command? For example, with the following code, at each iteration, the file name is overwritten each time and I want avoid that, preferring different names for each iteration using each index i for that:
using DelimitedFiles
for i=1:100
data=rand(50,1000)
writedlm("candidates.txt", data, " ")
end
How can I achieve that?
Just write e.g.:
writedlm("candidates_$i.txt", data, " ")
This syntax is using interpolation, see here for details.
(I assume this is what you needed)

Access file inside folder, matlab mac

I have about 300 files I would like to access and import in Matlab, all these files are inside 300 folders.
The first file lie in the directory users/matt/Documents/folder_1 with the filename line.csv the 2nd file lie in users/matt/Documents/folder_2 with filename line.csv
So I would like to import the data from the 300 line.csv files in Matlab so I can take the average value. Is this possible? I am using mac osx btw.
I know what do with the .csv file, but I have no clue how to access them efficiently.
This should work: All we are doing is generating the string for every file path using sprintf and the loop index i, and then reading the csv file using csvread and storing the data in a cell array.
for i = 1:300 % Loop 300 times.
% Full path pointing to the csv file.
file_path = sprintf('users/matt/Documents/folder_%d/line.csv', i);
% Read data from csv and store it in a cell array.
data{i} = csvread(file_path);
end
% Do your computations here.
% ...
Remember to replace the 300 by the actual number of folders that you have.

How to import a column of a CSV file into a Ruby array?

My goal is to import a one column of a CSV file into a Ruby array. This is for a self-contained Ruby script, not an application. I'll just be running the script in Terminal and getting an output.
I'm having trouble finding the best way to import the file and finding the best way to dynamically insert the name of the file into that line of code. The filename will be different each time, and will be passed in by the user. I'm using $stdin.gets.chomp to ask the user for the filename, and setting it equal to file_name.
Can someone help me with this? Here's what I have for this part of the script:
require 'csv'
zip_array = CSV.read("path/to/file_name.csv")
and I need to be able to insert the proper file path above. Is this correct? And how do I get that path name in there? Maybe I'll need to totally re-structure my script, but any suggestions on how to do this?
There are two questions here, I think. The first is about getting user input from the command line. The usual way to do this is with ARGV. In your program you could do file_name = ARGV[0] so a user could type ruby your_program.rb path/to/file_name.csv on the command line.
The next is about reading CSVs. Using CSV.read will take the whole CSV, not just a single column. If you want to choose one column of many, you are likely better off doing:
zip_array = []
CSV.foreach(file_name) { |row| zip_array << row[whichever_column] }
Okay, first problem:
a) The file name will be different on each run (I'm supposing it will always be a CSV file, right?)
You can solve this problem with creating a folder, say input_data inside your Ruby script. Then do:
Dir.glob('input_data/*.csv')
This will produce an array of ALL files inside that folder that end with CSV. If we assume there will be only 1 file at a time in that folder (with a different name), we can do:
file_name = Dir.glob('input_data/*.csv')[0]
This way you'll dynamically get the file path, no matter what the file is named. If the csv file is inside the same directory as your Ruby script, you can just do:
Dir.glob('*.csv')[0]
Now, for importing only 1 column into a Ruby array (let's suppose it's the first column):
require 'csv'
array = []
CSV.foreach(file_name) do |csv_row|
array << csv_row[0] # [0] for the first column, [1] for the second etc.
end
What if your CSV file has headers? Suppose your column name is 'Total'. You can do:
require 'csv'
array = []
CSV.foreach(file_name, headers: true) do |csv_row|
array << csv_row['Total']
end
Now it doesn't matter if your column is the 1st column, the 3rd etc, as long as it has a header named 'Total', Ruby will find it.
CSV.foreach reads your file line-by-line and is good for big files. CSV.read will read it at once but using it you can make your code more concise:
array = CSV.read(, headers: true).map do |csv_row|
csv_row['Total']
end
Hope this helped.
First, you need to assign the returned value from $stdin.gets.chomp to a variable:
foo = $stdin.gets.chomp
Which will assign the entered input to foo.
You don't need to use $stdin though, as gets will use the standard input channel by default:
foo = gets.chomp
At that point use the variable as your read parameter:
zip_array = CSV.read(foo)
That's all basic coding and covered in any intro book for a language.

Get line count before looping over data in ruby

I need to get the total number of lines that an IO object contains before looping through each line in the IO object. How can I do this in ruby?
You can't really, unless you want to shell out to wc and parse the result of that - otherwise you'll need to do two passes - one to get the line numbers, and another to do your actual work.
(assuming we're talking about a File IO instance - neither of those approaches work for network sockets etc)
in rails (the only difference is how I generate the file object instance)
file = File.open(File.join(Rails.root, 'lib', 'assets', 'file.json'))
linecount = file.readlines.size
io.lines.count would give you the number of lines.
io.lines.each_with_index {|line, index|} would give you each line and which line number it is (starting at 0).
But I don't know if it's possible to count the number of lines without reading a file.
You may want to read a file, and then use io.rewind to read it again.
If your file is not humongous, slurp it into memory(array) and count the the number of items( ie lines).

Resources