스크립트를 명령 줄에서 실행할 때 다른 변수에 명령 줄 인수를 할당하는 Python 스크립트가 있습니다. 조금 전에 문제없이이 작업을 해봤지만, 이번에는 약간의 기술적 인 부분에 매달려 있습니다. 쉽게 풀 수는 없습니다. 6. 통해 내가 기대하고있어 명령 줄 인수가 정수의 문자열입니다 Python 동등성 연산자가 예상대로 작동하지 않습니다.
n_value = sys.argv[3]
, 1 다음, 나중에 내가 원하는 :
의 내가 beginining 근처에 다음과 같은 문이 있다고 가정 해 봅시다 다음에 갈 방법을 결정하기 위해 n_value가 가리키는 값을 테스트합니다. 그래서, 다음과 같습니다 :
if n_value == "1":
(do something)
마지막으로, 명령 줄 인수 입력이 예상되지 않은 인스턴스에 대한 else 문이 있습니다. 나는이 프로그램을 실행하려고 할 때마다 이것을 얻는다. 나는 사물의 무리를 시도했습니다
if n_value is "1"
등,하지만 난 그 값을 만들 수없는 것 :
if n_value == 1:
또는 : 나는 같은 말을 할 수있는 if 문을 변경 시도했습니다 참된.
또한이 문장 앞에 set_trace()를 사용하여 pdb를 사용해 보았습니다. 디버거 내에서 식 n_value == "1"의 값을 살펴보고 "True"라고 말합니다. 이것은 제가 사용하고있는 버전에 기술적 인 문제가 있다고 생각하게합니다 (즉, 나는 잘못된 것을하고 있으며 그것을 깨닫지 못합니다). 또는 파이썬 동등성의 내용을 이해하지 못합니다. 작업.
마지막주의 사항 : 사용하고있는 Python 버전은 2.6과 2.7입니다. 내가 말할 수있는 한, 같은 문제.
관심을하면, 아래에있는 내 main() 메소드의 시작을 참조하십시오 if n_value == '1'
후
def main():
if len(sys.argv) != 4:
print(r'usage: python(2.6) ./log_likelihood_ngrams.py /path/to/input_file1 /path/to/input_file2 n_value')
sys.exit(1)
# Store command-line arguments as variables
input_file1_path = sys.argv[1]
input_file2_path = sys.argv[2]
n_value = sys.argv[3]
# Tokenize the input files and save their n-grams in n-gram-lists
# For 1-grams
if n_value == '1':
ngrams_list1 = tokenize(input_file1_path)
ngrams_list2 = tokenize(input_file2_path)
# For 2-grams
if n_value == '2':
ngrams_list1 = bigram_list(input_file1_path)
ngrams_list2 = bigram_list(input_file2_path)
# For 3-grams
if n_value == '3':
ngrams_list1 = trigram_list(input_file1_path)
ngrams_list2 = trigram_list(input_file2_path)
# For 4-grams
if n_value == '4':
ngrams_list1 = four_gram_list(input_file1_path)
ngrams_list2 = four_gram_list(input_file2_path)
# For 5-grams
if n_value == '5':
ngrams_list1 = five_gram_list(input_file1_path)
ngrams_list2 = five_gram_list(input_file2_path)
# For 6-grams
if n_value == '6':
ngrams_list1 = six_gram_list(input_file1_path)
ngrams_list2 = six_gram_list(input_file2_path)
# If n is invalid, print an error message and exit the program.
else:
sys.stderr.write('\n\nThe value of n you entered is not valid!\nPlease enter a value between 1 and 6, inclusive.\n')
sys.exit(1)
다른 인코딩을 비교하려고하는지 확인 했습니까? 또한 str (1)을 시도 했습니까? –
'repr (nvalue)'를 출력하십시오. 대답을 알 수 있습니다 :-) –
@VikramSaran 예, 있습니다. 나는 초기 할당을 n_value = str (sys.argv [3])과 n_value = sys.argv 다음에 n_value = str (n_value)로 변경하려고 시도했다. (단지 실제 sys.argv 메소드를 typecasting 할 때 문제가 발생했기 때문이다.). 또한 int()를 사용하여 typecasting을 시도하고 그 정수의 문자열이 아닌 실제 정수로 값을 변경했습니다. – user3029690