파이썬 유닉스 명령어 실행 방법..

조회수 621회

안녕하세요.. 최근에 파이썬 입문한 파린이 입니다.

결과값을

tldextract [결과값] | sort | uniq | grep .com | tr -d ','

이렇게 하여

twitter com 
weheartit com 
www facebook com
www pinterest com
www reddit com
www youtube com

이런식으로 도메인에 대한 정규식 정보만 뽑고 싶은데요, 파이썬에서 유닉스 정규식 표현 사용하는 법을 잘 모르겠습니다.

<원래 코드 >

 import requests, json, os

 url = "http://localhost:5000/search"

 data = {
     "image_url":"http://www.mrtt.news/news/photo/201806/147_281_1930.jpg",
     "resized_images":True # Or true
 }


 headers = {'Content-type': 'application/json'}
 r = requests.post(url, headers=headers, data=json.dumps(data))              

 //r.json to get the response as json
 print(r.json())

위 코드 실행후 결과값을

tldextract [결과값] | sort | uniq | grep .com | tr -d ','

으로 얻어내고 싶어서 찾아보니 subprocess라는 모듈이 유닉스 코드를 사용 할 수 있도록 도와준다고 하더라구요, 그래서 하기와 같이 코드를 수정해 보았는데,

AttributeError: module 'subprocess' has no attribute 'a' 

이런 에러가 나오네요

하기는 제가 수정 해 본 코드 입니다

cat example1.py

import requests, json
import subprocess

url = "http://localhost:5000/search"

data = {
    "image_url":"http://www.mrtt.news/news/photo/201806/147_281_1930.jpg",
    "resized_images":True # Or true
}


headers = {'Content-type': 'application/json'}
r = requests.post(url, headers=headers, data=json.dumps(data))

//r.json to get the response as json
a = r.json()
imageURL = subprocess.a
image = imageURL.check_output('| sort | uniq | grep .com | tr -d ','')
print(image)

어떻게 수정을 해야 제가 원하는 값을 뽑을 수 있을까요??

1 답변

  • tldextract 는 모듈로 제공되므로 프로세스를 포크할 필요없이 하기의 예제와 같이 사용할 수 있습니다.

    >>> import tldextract
    >>> tldextract.extract('http://forums.news.cnn.com/')
    ExtractResult(subdomain='forums.news', domain='cnn', suffix='com')
    >>> tldextract.extract('http://forums.bbc.co.uk/') # United Kingdom
    ExtractResult(subdomain='forums', domain='bbc', suffix='co.uk')
    >>> tldextract.extract('http://www.worldbank.org.kg/') # Kyrgyzstan
    ExtractResult(subdomain='www', domain='worldbank', suffix='org.kg')
    

    포크해서 사용해야한다면 아래의 형식으로 사용합니다.

    • 원형
    subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)
    
    import subprocess
    subprocess.call(["ls", "-l"])
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)