Create 2d array from csv file in python 3 - arrays

I have a .csv file which contains data like the following example
A,B,C,D,E,F,
1,-1.978,7.676,7.676,7.676,0,
2,-2.028,6.081,6.081,6.081,1,
3,-1.991,6.142,6.142,6.142,1,
4,-1.990,65.210,65.210,65.210,5,
5,-2.018,8.212,8.212,8.212,5,
6,54.733,32.545,32.545,32.545,6,
..and so on
The format is constant.
I want to load the file in a variable "log" and access them as log[row][column]
example
log[0][2] should give C
log[3][1] should give -1
If use this code
file = open('log.csv')
log = list(file)
when i use this code i get only one dimensional. log[row]
Is their any way to directly store them?
for example
read the file
split with '\n'
split again with ','
Thanks!!

Try this
log = [line.split(',') for line in open('log.csv')]

Related

How to read Json file(array values) and print values using shell script

I have a Jenkins Job that prints a list of elements either in Json or Text format,I am trying to setup a Jenkins job that would print the first element ,second element..... form a JSON or TEXT.And I wanted to assign it to a variable so i can retrieve any element using its number, "ex:- array[4]" I am looking to read the output and print these values in shell.
and I am trying to use different ways from online to get to ,using arrays ,using echo and yet looking for other way, any documents or any information would be helpful for me to go through.
the output on Jenkins in JSON looks like:-
[
"Danilo"
]
[
"Kevin"
]
the output on Jenkins in TEXT looks like:-
Danilo
Kevin
Any information or a place would be helpful. Thank you!
I have used for loop by getting the count of the words.And the output I stored in a txt file not json.
so i used c1=$(wc-w < file.txt) file.txt is the name of the file used to input these values from example Danilo,Kevin,and c1 gives number of word count which is 2 in this example
Now I used for loop:
for i in `seq 1 $c1`
do
export Name$i=$(sed -n "$i"p file.txt)
done
this way we will get the output as Name1=Danilo and Name2=Kevin as I am exporting the value each time in the loop.

read value file in Matlab

Currently I am using read comma-separated value (CSV) file which is csvread in my code. what if I want to read value row by row, which my value is not seperated by comma?
This is my code for save data:
%% Save data to .txt
fid=fopen('MyFile1.txt','w');
fprintf(fid, '%f \n', AngleValue');
fclose(fid);
This is the save values
Then I run a code using csvread
I = csvread('MyFile1.txt');
but I got an error. can anyone give an idea, probably I should change how I save my data or how can I read the data row by row. Thanks
There are other alternatives to csvread. Try dlmread instead
I = dlmread('MyFile.txt');
A more general approach,
Why are you writing AngleValue as text files? Why not use save and load in a more Matlab-ish fashion? read and write in binary format, tailored for Matlab use?
save('MyFile.mat','AngleValue'); %// save to binary Mat file
Once you saved the variable, you can read it:
load('MyFile.mat'); %// that's it!
use
A = fscanf(fileID,formatSpec)
more info
Problem solved. I found out that i just have to put coma (,) when i fprintf my saved data.
My previous code:
fprintf(fid, '%f \n', AngleValue');
My corrected code:
fprintf(fid, '%f,', AngleValue');
So now i can use csvread in my program.

Read LINQ results

I'm writing a small application that will download a .csv file via FTP and read that into Excel in a particular format.
I found the LINQ code snippet below on this site that will read the .csv file into the var 'csv'. The problem is that I can't seem to figure out how to enumerate the var 'csv' into a string array (which I'll then use to populate the relevant Excel cells).
Can anyone help? Thanks, Gavin
var lines = File.ReadAllLines(lblShowFileName.Text).Select(a => a.Split(','));
var csv = from line in lines select (from piece in line select piece);
Use ToArray() extension method to generate string[] instead of IEnumerable<string>
var csv = (from line in lines
select (from piece in line
select piece).ToArray()).ToArray();
Because you're calling ToArray() twice - within inner and outer query, your csv variable will be jagged array of strings: string[][]

open text file, modify text, place into sql database with groovy

I have a text file that has a large grouping of numbers (137mb text file) and am looking to use groovy to open the text file, read it line-by-line, modify the numbers, and then place them into a database (as strings). There are going to be 2 items per line that need to be written to separate database columns, which are related.
My text file looks as such:
A.12345
A.14553
A.26343
B.23524
C.43633
C.23525
So the flow would be:
Step 1.The file is opened
Step 2.Line 1 is red
Step 3.Line 1 is split into letter/number pair [:]
Step 4.The number is divided by 10
Step 5.Letter is written to letter data base (as string)
Step 6.Number is written to number database (as string)
Step 7.Letter:number pair is also written to a separate comma separated text file.
Step 8.Proceed to next line (line 2)
Output text file should look like this:
A,1234.5
A,1455.3
A,2634.3
B,2352.4
C,4363.3
C,2352.5
Database for numbers should look like this:
1:1234.5
2:1455.3
3:2634.3
4:2352.4
5:4363.3
6:2352.5
*lead numbers are database index locations, for relational purpose
Database for letters should look like this:
1:A
2:A
3:A
4:B
5:C
6:C
*lead numbers are database index locations, for relational purpose
I have been able to do most of this; the issue I am running into is not be able to use the .eachLine( line -> ) function correctly... and have NO clue how to output the values to the databases.
There is one more thing I am quite dense about, and that is the instance where the script encounters an error. The text file has TONS of entries (around 9000000) so I am wondering if there is a way to make it so if the script fails or anything happens that I can restart the script from the last modified line.
Meaning, the script has an error (my computer gets shut down somehow) and stops running at line 125122 (completes modification of line 125122) of the text file... how do I make it so when I start the script the second time run the script at line 125123.
Here is my sample code so far:
//openfile
myFile = new File("C:\\file.txt")
//set fileline to target
printFileLine = { it }
//set target to argument
numArg = myFile.eachLine( printFileLine )
//set argument to array split at "."
numArray = numArg.split(".")
//set int array for numbers after the first char, which is a letter
def intArray = numArray[2] { it as int } as int
//set string array for numbers after the first char, which is a letter
def letArray = numArray[1] { it as string }
//No clue how to write to a database or file... or do the persistence thing.
Any help would be appreciated.
I would use a loop to cycle over every line within the text file, I would also use Java methods for manipulating strings.
def file = new File('C:\\file.txt')
StringBuilder sb = new StringBuilder();
file.eachLine { line ->
//set StringBuilder to new line
sb.setLength(0);
sb.append(line);
//format string
sb.setCharAt(1, ',');
sb.insert(5, '.');
}
You could then write each line to a new text file, example here. You could use a simple counter (e.g. counter = 0; and then counter++;) to store the latest line that has been read/written and use that if an error occurs. You could catch possible errors within a try/catch statement if you are regularly getting crashes also.
This guide should give you a good start with working with a database (presuming SQL).
Warning, all of this code is untested and should hopefully give you more direction. There are probably many other ways to solve this differently, so keep an open mind.

J2ME Writing in CSV file through file Connection API

I am creating an application in which i need to add a new column in the csv file and then entries for that particular column.
And I have tried OutputStream and PrintStream but the problem is that the data is being written in starting of the file but i want the data at random position according to my need.
And RandomAccessFile is not identified by the application.
For e.g.
My CSV is:
Name,any_date
A,
B,
c,
And after writing it will look like
Name,any_date
A,p
B,a
C,p
I am using file Connection API to read and write.Can anyone suggest me how to do that??
Thanks in advance.
i think you want to append data in the file.
you can try this
os = fconn.openOutputStream(fconn.fileSize());
os.write(data.getBytes());
This is a simple example to append data at last.

Resources