convert php script to python
convert php script to python is project number 234080 posted at GetAFreelancer.com. Click here to post your own project.
| Status: |
Closed
(Selected Service Provider)
|
| Selected Providers: |
selected service provider
ke888le
|
| Budget: |
$30-250
|
| Created: |
03/06/2008 at 3:42 EST |
| Bidding Ends: |
03/07/2008 at 3:42 EST
|
| Project Creator: |
willie108
Buyer Rating:           (3 reviews)
|
| Description: |
The maximum I will pay for this is $35. I have a php script that results in a comma delimited file. I want to convert it to python:
The following code outputs a comma delimited file . The sql query that is used is "SELECT count( wordlocation.wordid ) AS amount , wordlist.word , urllist.url FROM wordlocation , wordlist , urllist WHERE wordlist.rowid = wordlocation.wordid AND urllist.rowid = wordlocation.urlid GROUP BY urlid , wordid";
I was wondering if you could write this for python so that it deposits a comma delimited file in a subfolder of the folder where this script is run called FREQUENCIES. (The folder does not exist so it should be created)
Here is all the code in PHP (it also gives an html output but that is not necessary):
<html> <body> <?php
// database connection parameters define('DB_HOST', 'localhost'); define('DB_USER', ''); define('DB_PASS', ''); define('DB_DATABASE', 'test.db');
define('DELIMITER', ','); // delimiter for CSV files // filename where results should be written, make sure you have write permissions to that file define('RESULTS_FILE', 'results.csv'); // specify output type, can be either HTML or CSV define('OUTPUT_TYPE', 'HTML');
// function returns amount of words for url function getAmount($matrix, $url, $word) { for ($i=0; $i<count($matrix); $i++){ if ($matrix[$i]['url'] == $url && $matrix[$i]['word'] == $word){ return $matrix[$i]['amount']; } } return 0; }
// outputs HTML table function getHTMLOutput($matrix, $words, $urls) { $output = '<table border="1" cellpadding="4">'; $output .= '<tr>'; $output .= '<th> </th>'; foreach($words as $key => $word){ $output .= '<th>'.$word.'</th>'; } $output .= '</tr>';
foreach($urls as $key => $url){ $output .= '<tr>'; $output .= '<td>'.$url.'</td>'; foreach($words as $key2 => $word){ $output .= '<td>'.getAmount($matrix, $url, $word).'</td>'; } $output .= '<tr>'; } $output .= '</table>'; return $output; }
// outputs CSV formated file function getCSVOutput($matrix, $words, $urls) { $csv = fopen(RESULTS_FILE, 'w'); $line = array_merge(array(' '), $words); fputcsv($csv, $line, DELIMITER);
foreach($urls as $key => $url){ $line = array(); $line[0] = $url; foreach($words as $key2 => $word){ $line[] = getAmount($matrix, $url, $word); } fputcsv($csv, $line, DELIMITER); } fclose($csv); return '<a href="'.RESULTS_FILE.'">'.RESULTS_FILE.'</a>'; }
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS);
if (!$conn) { echo "Unable to connect to DB: " . mysql_error(); exit; }
if (!mysql_select_db(DB_DATABASE)) { echo "Unable to select database: " . mysql_error(); exit; }
$query = "SELECT count( wordlocation.wordid ) AS amount , wordlist.word , urllist.url FROM wordlocation , wordlist , urllist WHERE wordlist.rowid = wordlocation.wordid AND urllist.rowid = wordlocation.urlid GROUP BY urlid , wordid";
$result = mysql_query($query);
if (!$result) { echo "Could not successfully run query ($query) from DB: " . mysql_error(); exit; }
if (mysql_num_rows($result) == 0) { echo "No rows found, nothing to print, exiting"; exit; }
$words = array(); $urls = array(); $matrix = array();
while ($row = mysql_fetch_assoc($result)) { $words[] = $row['word']; $urls[] = $row['url']; $matrix[] = $row; }
mysql_free_result($result);
// extract only unique words and urls $words = array_unique($words); $urls = array_unique($urls);
if (OUTPUT_TYPE == 'CSV'){ // CSV OUTPUT echo getCSVOutput($matrix, $words, $urls); } else{ // HTML OUTPUT echo getHTMLOutput($matrix, $words, $urls); }
?> </body> </html>
|
| Job Type: |
|
| Database: |
Other
|
| Operating system: |
(None)
|
| Bid count: |
3
|
| Average bid: |
$ 35
|
|

|