Reply
Formatted Text to CSV. HELP!!!
Old 11-21-2007, 06:56 PM Formatted Text to CSV. HELP!!!
Junior Talker

Posts: 1
I need help, I am trying to pretty much convert a text file to a CSV file so I can import it into MySQL. And would also like to have a date added to every line.

The data that I am working with like this.

0.405 19417.379 346.651 51394.910
0.905 19417.320 346.544 51394.848
1.405 19417.367 346.576 51394.891
1.905 19417.355 346.597 51394.910
2.405 19417.367 346.533 51394.867
2.905 19417.447 346.629 51394.910
3.405 19417.436 346.651 51394.805
3.905 19417.389 346.640 51394.738
4.405 19417.400 346.587 51394.812

It needs to be formatted a little like this, and have the Data's data inserted in the front.

<date>,0.405,19417.379,346.651 51394.910
<date>,0.905,19417.320,346.544 51394.848
<date>,1.405,19417.367,346.576 51394.891
<date>,1.905,19417.355,346.597 51394.910
<date>,2.405,19417.367,346.533 51394.867
<date>,2.905,19417.447,346.629 51394.910
<date>,3.405,19417.436,346.651 51394.805
<date>,3.905,19417.389,346.640 51394.738
<date>,4.405,19417.400,346.587 51394.812


I and pretty new to PHP but have been able to write a PHP script that will do all the data processing I need on the data but, I need a better way of importing the data in to MySQL.

I have been using excel 2007 with the text to column function, To do what is needed on this.

The reason I am needed a way to automate this, is the fact that I have about 3-4GB of plain text that needs to formatted and imported in a DB.

Thanks for reading and hope you can help.
SILNTBOB is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 11-21-2007, 07:23 PM Re: Formatted Text to CSV. HELP!!!
tripy's Avatar
Fetchez la vache!

Posts: 1,924
Name: Thierry
Location: In the void
This should do...
PHP Code:
<?php
//First, connect to your DB

//Then open the file containing your datas
$h=fopen("file.txt","r");
if(
$h){
  
//We could open the file, we now parse each lines sequentially until the end of the file
  
while(false!==$buffer=fgets($h)){
    
//Generate the date. I use an unix timestamp here, 
    //but look for the date() reference to create what you want
    
$dt=time();
    
//We split every columns of the file on the "space" character, specifying that it must containg 3 elements
    //Meaning that the last field will contains a blank between the 2 series of numbers, like your example
    
$ary=explode(" ",$buffer,3);
    
//Create your SQL statement
    
$sql="INSERT INTO table (dt, field1, field2, field3) VALUES ('$dt', '{$ary[0]}','{$ary[1]}','{$ary[3]}');";
    
//And fire up the query
    
mysql_query($sql);
  }
}
?>
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Formatted Text to CSV. HELP!!!
 

Thread Tools

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

vB 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 0.10951 seconds with 13 queries