简体   繁体   English

函数的python用户输入

[英]python user input to the function

i have 2 functions which one of them loading and preparing the data to the test我有 2 个函数,其中一个函数加载和准备数据以进行测试

and the other one performing the test , which is reading the file as a test and generate另一个执行测试,即读取文件作为测试并生成

the second file as a test result第二个文件作为测试结果

what i want to do is user input something instead of the file and then perform the test我想要做的是用户输入一些东西而不是文件然后执行测试

first function:第一个功能:

def load_test_data(source, X_word_to_ix, max_len):
    f = open(source, 'r')
    X_data = f.read()
    f.close()
    X = [text_to_word_sequence(x)[::-1] for x in X_data.split('\n') if 0 < len(x) <= max_len]
    for i, sentence in enumerate(X):
        for j, word in enumerate(sentence):
            if word in X_word_to_ix:
                X[i][j] = X_word_to_ix[word]
            elif word in X_word_to_ix is None:
                X[i][j] = None
            else:
                X[i][j] = X_word_to_ix['UNK']
    return X

instead the source there should be a user input相反,源应该是用户输入

second function:第二个功能:

if len(saved_weights) == 0:
    print("The network hasn't been trained! Program will exit...")
    sys.exit()
else:
    X_test = load_test_data('test.txt', X_word_to_ix, MAX_LEN)
    X_test = pad_sequences(X_test, maxlen=X_max_len, dtype='int32')
    model.load_weights(saved_weights)

    predictions = np.argmax(model.predict(X_test), axis=2)
    sequences = []
    for prediction in predictions:
        sequence = ' '.join([y_ix_to_word[index] for index in prediction if index > 0])
        print(sequence)
        sequences.append(sequence)
    np.savetxt('test_result', sequences, fmt='%s')

now its clear i want user input instead reading the file as an input and generate a file as an output现在很清楚我希望用户输入而不是读取文件作为输入并生成文件作为输出

i want get input from user and give and output not to the file我想从用户那里得到输入,而不是给和输出到文件

Your question is not clear but as per my understanding you want to take inputs from the terminal(user) as a file name or anything you want to input you can use system arguments For that do this:您的问题尚不清楚,但根据我的理解,您希望将终端(用户)的输入作为文件名或您想要输入的任何内容,您可以使用系统参数为此执行以下操作:

import sys
filename=sys.argv[0]
print("arguments I want to pass:", filename)

You can add more number of arguments by changing number in argv like sys.argv[1].您可以通过更改 argv 中的数字(如 sys.argv[1])来添加更多参数。 While running the file use:运行文件时使用:

python file.py arg1, arg2

Your question is very unclear and also include your desire output.你的问题很不清楚,还包括你的愿望输出。 Use tkinter for dialog pop up for user input.使用 tkinter 弹出对话框供用户输入。 Pass the argument to second function.将参数传递给第二个函数。

Use class since you have multiple function.使用类,因为您有多种功能。 As per my understanding you want the first function to read the file is that correct?根据我的理解,您希望第一个读取文件的函数正确吗? Hence, you should use a dialogbox for a user to insert an input by using tkinter or just a simple user input request, you can use input('Please insert: ')因此,您应该使用对话框让用户通过使用 tkinter 或只是一个简单的用户输入请求来插入输入,您可以使用input('Please insert: ')

You can pass the input to your function.您可以将输入传递给您的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM