I am new to Python and am over complicating the coding on a project so I am starting with much smaller data sets in order to learn the process. My boss is having me compare two CSV files. The first CSV only contains the data 1,2,3,4,5,6 all in a single column. He wants me to set this CSV file as an array so I can compare the second CSV against it. The second CSV contains the data 3,5,6 all in a single column. The code should result in a print out of 1,2,4 as it is the only data not found in both CSV files.
I originally tried to write a code to import both CSV files and compare data without setting it as an array but this did not work so the first CSV file needs to be set as an array. The problem is I am not sure exactly how to do this with an array. This is what I have so far, any help anyone could give me would be greatly appreciated. I have been working on this project for a week now and am at a total loss, even with this simplified form.
import csv
temp_list = []
with open('1.csv','rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
In terms of psuedo-code, what you need to do here is import both csv files into two separate arrays, Array A and Array B for example.
Now what you need to do is compare each index position in one array, to each index position in the other array.
You need to create a nested loop, where the outer loop will choose an index position in A and then inner loop chooses a position in B.
After you check one index in A with each position in B, and no positions are the same, I suggest adding this value into a third array, C. You can check which positions are the same by using a boolean flag. When your code is done, C will have any values that don't exist in both A and B.
I suggest following these tutorials to learn more about python syntax:
https://www.w3schools.com/python/
Good luck
Related
I know that I am pretty confused about arrays and strings and have tried a bunch of things but I am still stumped. I have groups of data that I am pulling into various arrays. For example I have site locations coming from one source. Numerous cores can be at a single location. The cores can have multiple depths. So I am pulling all this data together in various ways and pushing it out into a single excel file for each core. I create a filename based on location id and core name and year the core was sampled. So it might look like ‘ID_14_CORE_Bu-2-MT-1991.xlsx’ and I am storing it to use with a xlswrite statement in a variable called “filename.” This is all working fine.
But now I want to keep track of what files I have created and when I created them in another EXCEL file. So I was trying to store the location, filename and the date it was processed into some sort of array so that I can use the xlswrite statement to push it all out after I have processed all the locations/cores/layers that might occur in the original input files.
As I start the program and look in the original input files I can figure out how many cores I have so I wanted to create some sort of array to hold the location, filename and the date together. I have tried to use a cell array (a = cell(numcores,3)) but that does not seem to work. I think I am understanding that the filename is actually a string array so each of the letters is trying to be assigned to a separate cell instead of just the cell in the second column.
I also have had problems trying to push the three values out to the summary EXCEL file as each core is being processed but MATLAB tends to treat single dimensional arrays as a row rather than a column so I am kind of confused there.
Below is what I want an array to end up like…but since I am developing the filename on the fly this seems to be more challenging.
ArraytoExcel = [“14”, “ID_14_CORE_Bu-2-MT-1991.xlsx”,”1/1/2018”;
“14”, “ID_14_CORE_Bu-3-MT-1991.xlsx”,”1/1/2018”;
“13”, “ID_13_CORE_Tail_33-1992.xlsx”,”1/1/2018”;]
Maybe I am just going about this the wrong way. Any suggestions would help.
Your question is a little confusing but I think you want to do something like the following. The variables inside of my example are static but from your question it sounds like you already have these figured out somehow.
numcores = 5; %.. Or however, you determine what you are procesing
ArraytoExcel = cell(numcores ,3);
for ii = 1:numcores
%These 3 things will need to determined by you in the loop
% and not be static like in this example.
coreID = '14';
filename = 'ID_14_CORE_Bu-2-MT-1991.xlsx'; %
dataProc = datestr(now,'mm/dd/yyyy');
ArraytoExcel(ii,:) = {coreID,filename,dataProc};
end
xlswrite('YourOutput.xls',ArraytoExcel)
I am newer to programming and I am trying to figure out how I can add all of the numbers from the B column together and all of the C column together and the D column together with those variables named x y and z respectively. I have been searching everywhere for an answer. All I have gotten is to read the first line of the csv file.
This is a screenshot of part of the CSV file:
Once you've read it in, you need to somehow split it up into actual columns. Check this function, that should help.
strtok()
Then you need to store it in some kind of array. Then index which elements in the array you want, and process them by using a cumulative sum. This might require a while loop until you've reached the last line of the file.
I will leave the actual code to you, but this should get you going.
I have list of values save in variables like. A= 1,2,3,4,5,6,7 and B = 4,5,6,73,2,3,2 //This may be Array or column. Easy one will be preffered
Note A and B values will be dynamic which will be get through some function.
I want to save A's and B's Values in Excel Sheet, like A's values in First column and B's values in 2nd Column.
I read about xlswrite but did not find any scenerio as i require.
And when They are saved then Again want to read them, and to save them in some other variables like C and D. I want to save Data of first column in C and second column data in D.
I read about C = xlsread('filename') but problem with it is. This save all the values in one variable. like if Excell have two columns. Those both will be saved in one variable C. But my requirment is one colum in one varibale and so on.
Take a look at the Documentation
Syntax for writing to excel file
xlswrite(filename,A,sheet,xlRange)
Code:
%// As your variable is a row vector, it is transposed to column vector
%// before writing, as you preferred
xlswrite('outputFileName.xlsx',[A.',B.'],1,'D2')
Note: Make sure that the excel file is closed while writing
Syntax for reading from excel file
num = xlsread(filename,sheet,xlRange)
Code:
%// reading the range into one temporary variable
temp = xlsread('outputFileName.xlsx',1,'D2:E7')
C = temp(:,1)
D = temp(:,2)
I know English is not your primary language, but please try to be as gramatically correct as you can, it will make your questions easier to understand, and easier to answer to.
As for your question, I think you want to use the additional parameter in the functions you found to specify the columns you are writing to:
xlswrite('sheet.xls', A, 'E3')
will write the data of A on the cells E3, ..., E9.
And when you want to retrieve it, you do the same:
C = xlsread('sheet.xls', 'E3-E9')
This will read E3 to E9 cells and put the value in C.
So I have a huge excel file with 210 columns from(A all the way to CK)-Each of these columns have 80000-300000 values.I want to read this into a MATLAB array.I have two problems:
1.Is there any way that I can loop through the letters iteratively(from A to CK)?
2.When I try to read the file as a whole it says no storage-but I am able to create a matrix of ones of size-300000*210....So Im a bit puzzled and dont know what to do..??
Thanks!!
Save in the .csv format from EXCEL, then use load -ascii in matlab.
You can loop through the Excel column with the option xlRange of xlsread (doc).
And both the output num = xlsread(...) and [num,txt,raw] = xlsread(...) duplicate the information being read in several variables (type edit xlsread in the command window).
i have a code that works on all files in folder and analyise it and come then export results into excel sheet.
My question is, by the end i alot of excel sheets and i need to combine them all together (i also need them separate) so i do this manualy and its really painfull and time wasting !
So i want to ask is there a way to create an array to save the results of each analysis my code process and export all at the end to one excel file.
Example of my code Workflow:
Read files in current dir
Open files one by one.
Do math equations and get results into 2 arrays amp[] signal[] (both are equal in size)
Export results into Excel using
xlswrite([b '.xls'],[amp' signal'],1);%%b=file name (example.avi ==> example.avi.xls)
And so on.
So i want to save the results after that to array with the filename and at the end past all into 1 excel sheet with the file name on top of each column :)
I dont know why i'm having a brain block on how to do it !
Thanks alot for your help in advance
Xlswrite supports that you give a start point or range where you want to put your data. See Xlswrite.
You can quickly calculate the column name using this.
But you must be aware that excel only supports 255 columns and 1024 rows in a sheet. This happened often to me and then one gets a very uninformative error.