2017-08-14 10 views
0

私はソフトウェア(Maltego)から入力を受け取り、変数としてsys.argv [1]を使用するスクリプトを作成しています。 Maltegoからの情報がヘブライ語であるとき、私は実際のテキストではなく疑問符を得る。 テキストをさまざまな方法でエンコードしようとしましたが、すべてが失敗しました。Pythonでのsys.argvのエンコード2.7

私はPython 2.7を使用しています。

解決策についてのご意見をいただければ幸いです。

スクリプトがMaltego内から実行されている。ここで

# -*- coding: utf-8 -*- 

import time 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from bs4 import BeautifulSoup 
from MaltegoTransform import * 
import codecs 
import sys 
reload(sys) 
sys.setdefaultencoding('utf-8') 


me = MaltegoTransform() 
search_value = sys.argv[1].encode("utf-8") 
+0

我々は入力フォーマットとヘルプを提供するために、あなたのコードを知っている必要があります。 – EsotericVoid

+0

あなたのコードは今どのように見えますか?引数を解析するスニペットだけです。 –

+0

[sys.argvがUnicodeであることをPythonにどのように伝えるのですか?](https://stackoverflow.com/questions/5113618/how-do-i-tell-python-that-sys-argv-is- in-unicode) –

答えて

1

私の50ペンス:

import sys 


def win32_utf8_argv(): 
    """Uses shell32.GetCommandLineArgvW to get sys.argv as a list of UTF-8 
    strings. 

    Versions 2.5 and older of Python don't support Unicode in sys.argv on 
    Windows, with the underlying Windows API instead replacing multi-byte 
    characters with '?'. 

    Returns None on failure. 


    """ 

    try: 
     from ctypes import POINTER, byref, cdll, c_int, windll 
     from ctypes.wintypes import LPCWSTR, LPWSTR 

     GetCommandLineW = cdll.kernel32.GetCommandLineW 
     GetCommandLineW.argtypes = [] 
     GetCommandLineW.restype = LPCWSTR 

     CommandLineToArgvW = windll.shell32.CommandLineToArgvW 
     CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)] 
     CommandLineToArgvW.restype = POINTER(LPWSTR) 

     cmd = GetCommandLineW() 
     argc = c_int(0) 
     argv = CommandLineToArgvW(cmd, byref(argc)) 
     if argc.value > 0: 
      # Remove Python executable if present 
      if argc.value - len(sys.argv) == 1: 
       start = 1 
      else: 
       start = 0 
      return [argv[i].encode('utf-8') for i in 
        xrange(start, argc.value)] 
    except Exception: 
     pass 

if __name__ == '__main__': 
    a = win32_utf8_argv() 
    print (a[1])