Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
How to get a csv file into an array ?
Old 06-13-2010, 05:14 PM How to get a csv file into an array ?
Novice Talker

Posts: 6
Name: Levi Dhuyvetter
Trades: 0
Hello,

I have a csv file (http://finance.yahoo.com/d/quotes.csv?s=AAPL&f=sncl1).
The csv file look something lik this :
"AAPL","Apple Inc.","+3.00 - +1.20%",253.51
Now i want an array where is have this strings :
- AAPL
- Apple Inc.
- +3.00
- +1.20%
- 253.51

Does someone knows how to do that ?
levidhuyvetter is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-13-2010, 05:25 PM Re: How to get a csv file into an array ?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Not with javascript!


Or rather it can be done but browser security will not allow it.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?

Last edited by chrishirst; 06-13-2010 at 05:26 PM..
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 06-14-2010, 07:07 AM Re: How to get a csv file into an array ?
Novice Talker

Posts: 6
Name: Levi Dhuyvetter
Trades: 0
Quote:
Originally Posted by chrishirst View Post
Not with javascript!


Or rather it can be done but browser security will not allow it.
it's not for a browser application its for a mac dashboard widget
levidhuyvetter is offline
Reply With Quote
View Public Profile
 
Old 06-14-2010, 08:30 AM Re: How to get a csv file into an array ?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Then you will have to iterate through each fields, after having first split on the comma, to do the needed split on the minus in the third field.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 06-14-2010 at 08:43 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-14-2010, 08:38 AM Re: How to get a csv file into an array ?
Junior Talker

Posts: 1
Name: sandy
Trades: 0
There is another option you can use to place lines of text into an array. In the technique below, we're using the explode( ) string function to create an array from each line of text. Here's the code

<?PHP $file_handle = fopen("dictionary.txt", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode('=', $line_of_text);

print $parts[0] . $parts[1]. "<BR>";
}

fclose($file_handle);
?>
The lines to examine are in blue; the rest you have met before (get a file handle, loop round, use fgets to read the line). The first line to note is this:
$parts = explode('=', $line_of_text);
If you remember the string section, you'll also be familiar with the explode function. It splits a line of text, based on whatever you have provided for the separator. In our code, we have used the equals sign ( = ) as a separator. This is because each line in the dictionary.txt file looks like this:
AAS = Alive and smiling
When the explode function is executed, the variable called $parts will be an array. In our text file there will only be two positions in the array, one for each half of the equals sign.
We then print out both parts of the array with this:
print $parts[0] . $parts[1]. "<BR>";
So $parts[0] will hold the abbreviation (AAS) and $parts[1] will hold the meaning.
The next time round the while loop, the second line will be read from the text file. Exactly the same thing happens, so the line will be split again, and placed into an array. This is a good technique to use, if you want to split each line and do something different with each part of the line.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
sandyvong is offline
Reply With Quote
View Public Profile
 
Old 06-14-2010, 08:40 AM Re: How to get a csv file into an array ?
Novice Talker

Posts: 6
Name: Levi Dhuyvetter
Trades: 0
Quote:
Originally Posted by tripy View Post
Then don't use CSV.
Use the json (preferred) version or the XML (less preferred, but still better than csv) and navigate through the structure.

Simply replace the .csv with .xml or .json in your yahoo finance url.
As for navigating through json:
http://www.json.org/js.html
http://www.hunlock.com/blogs/Masteri...t_Notation_%29
but how do i get the contents from the json-file in an array ?
levidhuyvetter is offline
Reply With Quote
View Public Profile
 
Old 06-14-2010, 09:32 AM Re: How to get a csv file into an array ?
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
but how do i get the contents from the json-file in an array ?
I modified my post after double checking.
In fact, they always send you CSV formatted info, even if you put an .xml in place of the .csv

To answer your question, you don't pull the content of the file in an array.
1) You use an ajax rfequest to get back the whole content.
2) you split() on the carriage return to get an array with all the lines
3) you iterate over the lines, and do a split() on the comma to get the fields
4) you iterate over the fields, and do a split on the 3rd field to get both wanted infos
5) you compose the display in your widget, with all the infos at your hand.

Basically, you have to split() recursively 3 times to get everything you need to compose your display.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 06-14-2010, 12:01 PM Re: How to get a csv file into an array ?
Novice Talker

Posts: 6
Name: Levi Dhuyvetter
Trades: 0
Quote:
Originally Posted by tripy View Post
I modified my post after double checking.
In fact, they always send you CSV formatted info, even if you put an .xml in place of the .csv

To answer your question, you don't pull the content of the file in an array.
1) You use an ajax rfequest to get back the whole content.
2) you split() on the carriage return to get an array with all the lines
3) you iterate over the lines, and do a split() on the comma to get the fields
4) you iterate over the fields, and do a split on the 3rd field to get both wanted infos
5) you compose the display in your widget, with all the infos at your hand.

Basically, you have to split() recursively 3 times to get everything you need to compose your display.
i hav this code :
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Naamloos document</title>
</head>

<body>
<script type="text/javascript">
if (window.XMLHttpRequest && !(window.ActiveXObject)) {
	try {
		request = new XMLHttpRequest();
	} catch(e) {
		request = false;
	}
} else if (window.ActiveXObject) {
	try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		request = false;
	}
}
if (request) {
	request.open("GET","http://finance.yahoo.com/d/quotes.xml?s=AAPL&f=sn",true);
	request.send("");
	document.getElementById(text).innerHTML = request.responseText;
}
</script>
<div id="text"></div>
</body>
</html>
But when i watch it in my browser i don't see anything.
levidhuyvetter is offline
Reply With Quote
View Public Profile
 
Old 06-19-2010, 07:27 AM Re: How to get a csv file into an array ?
Junior Talker

Posts: 2
Name: Amit Agrawal
Location: Indore
Trades: 0
  1. String [][] numbers = new String [24][24];
  2. File file = new File("Currency Exchange Rates.csv");
  3. BufferedReader bufRdr = new BufferedReader(new FileReader(file));
  4. String line = null;
  5. int row = 0;
  6. int col = 0;
  7. //read each line of text file
  8. while((line = bufRdr.readLine()) != null && row < 24)
  9. {
  10. StringTokenizer st = new StringTokenizer(line,",");
  11. while (st.hasMoreTokens())
  12. {
  13. //get next token and store it in the array
  14. numbers[row][col] = st.nextToken();
  15. col++;
  16. }
  17. col = 0;
  18. row++;
  19. }.

that is java code to get a csv file into an array
__________________

Please login or register to view this content. Registration is FREE
&
Please login or register to view this content. Registration is FREE
VirtualAssist is offline
Reply With Quote
View Public Profile Visit VirtualAssist's homepage!
 
Reply     « Reply to How to get a csv file into an array ?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.32513 seconds with 12 queries