2013-12-13 3 views
1

다음은 코드 작성 전의 원시 데이터입니다.Parsed Json 텍스트에서 한 단어 만 인쇄하십시오.

: 여기
import csv 
import json 
import oauth2 as oauth 
import urllib 
import sys 
import requests 
import time 

CONSUMER_KEY = "" 
CONSUMER_SECRET = "" 
ACCESS_KEY = "" 
ACCESS_SECRET = "" 

class TwitterSearch: 
    def __init__(self, 
     ckey = CONSUMER_KEY, 
     csecret = CONSUMER_SECRET, 
     akey = ACCESS_KEY, 
     asecret = ACCESS_SECRET, 
     query = 'https://api.twitter.com/1.1/search/tweets.{mode}?{query}' 
    ): 
     consumer  = oauth.Consumer(key=ckey, secret=csecret) 
     access_token = oauth.Token(key=akey, secret=asecret) 
     self.client = oauth.Client(consumer, access_token) 
     self.query = query 

    def search(self, q, mode='json', **queryargs): 
     queryargs['q'] = q 
     query = urllib.urlencode(queryargs) 
     return self.client.request(self.query.format(query=query, mode=mode)) 

def write_csv(fname, rows, header=None, append=False, **kwargs): 
    filemode = 'ab' if append else 'wb' 
    with open(fname, filemode) as outf: 
     out_csv = csv.writer(outf, **kwargs) 
     if header: 
      out_csv.writerow(header) 
     out_csv.writerows(rows) 

def main(): 
    ts = TwitterSearch() 
    response, data = ts.search('@gmail.com', result_type='recent') 
    js = json.loads(data) 
    search_terms = ['@gmail.com'] 
    matches = [] 
    for term in search_terms: 
     match = [word for word in js if term in word] 
     matches.append(match) 
    messages = ([msg['created_at'], msg['text'], msg['user']['id'], matches[0]] for msg in js.get('statuses', [])) 
    write_csv('twitter_gmail.csv', messages, append=True) 

if __name__ == '__main__': 
    main() 

은 .CSV에있는 출력 :
{"metadata":{"result_type":"recent","iso_language_code":"et"} 
"created_at":"Tue Dec 03 01:41:53 +0000 2013","id":407686093790662656,"id_str":"407686093790662656","text":"@emblems123 [email protected]","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":407677310821613569,"in_reply_to_status_id_str":"407677310821613569","in_reply_to_user_id":2201997043,"in_reply_to_user_id_str":"2201997043","in_reply_to_screen_name":"emblems123","user":{"id":1220098345,"id_str":"1220098345","name":"PYD","screen_name":"bieberfan12599","location": 

나는 아래의 코드를 실행 (내 코드가 호출을 한 후 나는 트위터 API에서이 데이터를 얻을)
Fri Dec 13 03:49:06 +0000 2013,I need some HARD TRAP beats producers help me out [email protected],490060971,[] 

제 문제는 구문 분석 된 JS 텍스트의 전자 메일 주소 만 인쇄하려고합니다. 나는 split()을 시도했지만 표현으로 그 것을 할 수 없다. 내가하는 일과 상관없는 것처럼 보입니다. "[]"

"텍스트"의 일부분을 전자 메일로 출력하는 방법을 알고 싶습니다.

import re 
string = "Fri Dec 13 03:49:06 +0000 2013,I need some HARD TRAP beats producers help me out [email protected],490060971,[]" 
regex = "\[email protected]\w+\.com" 
match = re.findall(regex,string) 
print match 

이 경우 하나의의 모든 경기, 을 포함하는 출력 :

['[email protected]'] 

그리고

답변

0

당신은 다음 regex을 사용하여 이메일을 추출 할 수있는 문자열의 데이터를 가정하면 stringstr() 함수를 사용하여 dict을 문자열로 변환하여 얻은 원시 데이터 문자열로 바꾸는 경우에도

string = str({"metadata":{"result_type":"recent","iso_language_code":"et"}, 
      "created_at":"Tue Dec 03 01:41:53 +0000 2013","id":407686093790662656,"id_str":"407686093790662656","text":"@emblems123 [email protected]","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":False,"in_reply_to_status_id":407677310821613569,"in_reply_to_status_id_str":"407677310821613569","in_reply_to_user_id":2201997043,"in_reply_to_user_id_str":"2201997043","in_reply_to_screen_name":"emblems123","user":{"id":1220098345,"id_str":"1220098345","name":"PYD","screen_name":"bieberfan12599","location":"NY"}}) 
당신은 여전히 ​​예상 출력을 얻을 수있을 것입니다

:

['[email protected]'] 
관련 문제