-1

I'm using a python script to convert text file into csv and i want to call that python script from php to export those data into the data base.how can i call this python script from php ? the name of my python file is convertcsv.py. this is my python code

import csv

csv_file = r"mycsv.csv"

txt_file = r"newtext.txt"
lines = open( r"mytext.txt").readlines()
open(txt_file, 'w').writelines(lines[14:-1])

in_txt = csv.reader(open(txt_file, "rb"), delimiter = ',')
out_csv = csv.writer(open(csv_file, 'wb'))

out_csv.writerows(in_txt)

i need to call above python file from php and when calling this csv file should be created.this is working fine in python.

devuser
  • 171
  • 2
  • 5
  • 24
  • [Did](http://stackoverflow.com/questions/5497540/) [you](http://stackoverflow.com/questions/14047979/) [search](http://stackoverflow.com/questions/19735250/) [before](http://stackoverflow.com/questions/2219982/) [asking](http://stackoverflow.com/questions/18451272/)? – Amal Murali Apr 22 '14 at 03:30
  • my problem was i didnt configure Apache to run python.After doing configeration in httpd.conf i can call python scripts from php – devuser Apr 22 '14 at 05:02

2 Answers2

1
<?php

system('python convertcsv.py');

?>

On a side note, just don't use PHP and only use python.

Loïc whistles, leaving

Loïc
  • 11,804
  • 1
  • 31
  • 49
  • *acting non-chalant* Hey man, just use python *whistles the theme to Monty Python's Flying Circus* – fr1tz Apr 22 '14 at 03:33
0
<?php
            exec('python convertcsv.py');
?>
Gunjan
  • 2,775
  • 27
  • 30
  • i tried this.i didnt get any errors but mytext file not converts to csv.so it seemslike not calling the script. – devuser Apr 22 '14 at 04:10