无法在php脚本中通过python创建.csv文件

时间:2016-04-13 21:13:01

标签: php python csv

我有python代码,我从php脚本运行它。

PHP代码:

MyWebApplication
    project.json
    package.json
    ...
    -node_modules
    ...
    -wwwroot
    ...
       -ts
         -pages
              orders-page.ts
       tsconfig.json
       typings.json
    ...

Python代码:

<?php
if(isset($_POST['submit'])) /* i.e. the PHP code is executed only when someone presses Submit button in the below given HTML Form */
{
$var = $_POST['any_name'];   // Here $var is the input taken from user.
}

$last=exec('/home/ankit/anaconda/bin/python data_pull.py --q kunal',$output,$return);
echo $last;
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="text" name="any_name">
<input type="submit" name="submit">
</form>

实际上我在代码中编写csv文件以供进一步使用。代码在没有php的情况下工作正常,但是在php from apiclient.discovery import build #pip install google-api-python-client from apiclient.errors import HttpError #pip install google-api-python-client from oauth2client.tools import argparser #pip install oauth2client import pandas as pd #pip install pandas import csv import sys DEVELOPER_KEY = "key here" YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" #ALS Ice Bucket Challenge argparser.add_argument("--q", help="Search term", default = 'bollywood') #change the default to the search term you want to search argparser.add_argument("--max-results", help="Max results", default=40) #default number of results which are returned. It can vary from 0 - 100 args = argparser.parse_args() options = args youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) # Call the search.list method to retrieve results matching the specified # query term. search_response = youtube.search().list( q=options.q, type="video", part="id,snippet", maxResults=options.max_results ).execute() videos = {} # Add each result to the appropriate list, and then display the lists of # matching videos. # Filter out channels, and playlists. for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": #videos.append("%s" % (search_result["id"]["videoId"])) videos[search_result["id"]["videoId"]] = search_result["snippet"]["title"] #print "Videos:\n", "\n".join(videos), "\n" s = ','.join(videos.keys()) videos_list_response = youtube.videos().list( id=s, part='id,statistics,snippet' ).execute() res = [] for i in videos_list_response['items']: temp_res = dict(v_id = i['id'], v_title = videos[i['id']]) temp_res.update(i['statistics']) temp_res.update(i['snippet']) res.append(temp_res) pd.DataFrame.from_dict(res) print 'ankit is here3' #print res f=open('youtube.csv', 'w') w=csv.writer(f) print 'ankit is here3' """ for r in res: #with open('youtube.csv', 'wb') as f: # Just use 'w' mode in 3.x w = csv.DictWriter(f,r.keys()) w.writeheader() w.writerow(res) row=[] for key, value in res[0].iteritems() : row.append(key) w.writerow(row) print row """ #print res print 'ankit is here3' for r in res: row=[] small_row='' if 'commentCount' in r.keys(): item=r['commentCount'].encode("ascii") row.append(item) else: row.append('0') item=r['viewCount'].encode("ascii") #row.append(r['viewCount'].encode("ascii")) row.append(item) #row.append(r['v_title'].encode("ascii")) item=r['v_title'].encode('ascii','ignore') row.append(item) #row.append(r['dislikeCount'].encode("ascii")) if 'dislikeCount' in r.keys(): item=r['dislikeCount'].encode("ascii") row.append(item) else: row.append('0') #item=r['dislikeCount'].encode("ascii") #row.append(item) #row.append(r['v_id'].encode("ascii")) item=r['v_id'].encode("ascii") row.append(item) #row.append(r['favoriteCount'].encode("ascii")) item=r['favoriteCount'].encode("ascii") row.append(item) #row.append(r['likeCount'].encode("ascii")) #item=r['likeCount'].encode("ascii") #row.append(item) if 'likeCount' in r.keys(): item=r['likeCount'].encode("ascii") row.append(item) else: row.append('0') if 'tags' in r.keys(): for t in r['tags']: t=t.encode("ascii",'ignore') small_row = small_row + '/' + t row.append(small_row) else: row.append('0') #row.append(r['channelId']) #row.append(r['categoryId']) #row.append(r['thumbnails']) w.writerow(row) print row 文件夹中运行时它没有创建任何文件。

在php中运行时的输出是:

/var/www/html/

我没有收到错误。

0 个答案:

没有答案