2017-08-14 3 views
0

Ich schreibe ein Skript, das die Eingabe von einer Software (Maltego) und sys.argv [1] als Variable verwendet. Wenn die Informationen von Maltego auf Hebräisch sind, bekomme ich eher Fragezeichen als den eigentlichen Text. Ich habe versucht, den Text auf verschiedene Arten zu kodieren, aber alles ist fehlgeschlagen.Codierung sys.argv in Python 2.7

Ich benutze Python 2.7.

Jede Idee für eine Lösung wird sehr geschätzt.

das Skript ausgeführt von innen Maltego wird:

# -*- 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

Wir brauchen das Eingabeformat und Ihren Code zu wissen, Hilfe zu leisten. – EsotericVoid

+0

Wie sieht Ihr Code jetzt aus? nur der Ausschnitt, der Argumente analysiert. –

+0

Mögliches Duplikat von [Wie erkläre ich Python, dass sys.argv in Unicode ist?] (Https://stackoverflow.com/questions/5113618/how-do-itel-python-that-sys-argv-is- In-Unicode) –

Antwort

1

Hier meine fünfzig Pence:

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])