2012-11-06 2 views
0

내 스크립트가 행이 아닌 여러 열에 쓰는 문제에 봉착했습니다. 내가 뭘 잘못하고 있는지 잘 모르겠다.CSV에 쓰기 - 열이 아닌 행

공개 - 저는 프로그래밍을 처음 접했습니다.

편집 - 알아 냈어. 작동 함 :-)

의견이 있으십니까?

import json 
import csv 
import subprocess 
import urllib 
import requests 
from unidecode import unidecode 

def main(): 
    list_writer= open_csv() 
    info = test_method() 
    for list in info: 
     write_to_csv(list_writer, list) 


def test_method(): 
    r = requests.get('https://api.github.com/legacy/repos/search/python'+'?start_page=1', auth=('user', 'pass')) 
    dict_of_repos = json.loads(r.text) 
    list_of_repos = dict_of_repos["repositories"] 
    repo_information = [] 
    for repo in list_of_repos: 
     indiv_repo = [] 
     indiv_repo.append(repo["name"]) 
     indiv_repo.append(repo["fork"]) 
     print indiv_repo 
     repo_information.append(indiv_repo) 
    return repo_information 

def open_csv(): 
    github_csv = open('Githubcsv_1_2.csv', 'wb') 
    writer = csv.writer(github_csv) 
    return writer 

def write_to_csv(list_writer, info): 
    list_writer.writerow(info) 
    return 

if __name__ == '__main__': 
    main() 
+0

전체 프로그램을 제공하는 것보다 조금 더 좁혀보십시오. –

+0

각 행 다음에/n/r을 추가 할 수 있습니까? 또는 어떤 종류의 브레이크 라인 –

+0

코드를 간단히 스캔하면'writerow' 함수는 일반적으로 많은 열의 한 행을 쓰게됩니다. 이게 너가하려는거야? – Isaac

답변

0

그것은

[ 
    ['name'], 
    ['name2'], 
    ['name3'] 
] 

처럼 보일 것이다 repo_information처럼 보이는 당신이 필요로하는 모든이, 물론, 모든 데이터를 기록합니다 writerows

def write_to_csv(list_writer, info): 
    list_writer.writerows(info) 

에처럼 그렇게 보인다 하나의 열 (각 이름은 별도의 행에 있음). 당신은 당신이 할 수 동일한 행에 모두 기록 할 경우

for repo in list_of_repos:    
     repo_information.append(repo['name']) 
    print repo_information # ['name', 'name2', 'name3'] 
    return repo_information 

다음 writerow 당신이 현재하고있는 것처럼. 그러면 모든 이름이 같은 행에 배치됩니다.

+0

실제로 다른 행에 쓰고 싶습니다. 따라서 각 저장소 이름은 자체 행에 있습니다. –

+0

알아 냈어 - –

관련 문제