Easier way to get input data in Python

Back to Programming Languages forum

Lee Creighton     2020-05-13 22:06:51

I know this has been asked and answered, but in my opinion they're all incomplete.

Apparently the way to read in the large lists of data uses something like input.split(). However, I don't see anyone giving a complete solution to the problem, just hnts on how it would be done.

I am a beginner in Python and programming and would appreciate a more complete solution. That is, in one file, show mw where to put the list of numbers (at the beginning? at the end? in a separate file? I have no idea). Then, use the input() function to read in the lines one at the time.

I suppose the majoe thing missing in the helps on this is where to put the data. It's not clear and I can't see a way to do it.

Thanks for any help.

anneenna     2020-05-14 01:23:27

use I/O this is my template:

try:

myfile = open('input_val.txt')

except:

print('IO error')

else:

temp = myfile.read()

ins = temp.split('\n')

the Try/Catch is so in case the file is not present.

Alex Sau     2020-05-14 07:25:28

Hi Lee, as I can see, the majority of the test data is like:

5       --> number of tests, can be 5 or 700 or 3000+
stalede --> test 1
slatexa
parings
signers
traipse --> test n

Don't use input(), there will be some cases in which you'll have tons of data. I've just ran a test with 63000+ lines. :/ I always copy/paste the "test data" into a blank new file called "input.txt".

Then write a program in which you'll define a function to read from that file:

# My program for codeabbey

INPUT_FILE = './input.txt' # Challenge

Define a function to read that file as follows, you may use this function later:

def read_challenge(file):
    # Read the test data. Note that 1st line is a number, then words/numbers/tests
    # 
    # Return value: a list with all the words from the challenge

    with open(file, 'r') as my_file:
        data = []

        # Get the (int) at the very 1st line with readline()
        # and then do a "for" 'i' times
        # You'll have to convert to (int) this line
        # because everything you read from a file is a string (str)
        for i in range(int(input_file.readline())):

            # append every data-value to the list
            # Note: split() will remove blanks (including any '\n') from the string
            data.append(my_file.readline().split()[0])

    # the list with the data
    return data

Call the function like:

# read the file and put the list of data in the variable 'test_data'
codeabbey_data = read_challenge(INPUT_FILE)

You can always see what is in the list:

# print the data
print(codeabbey_data)

You can iterate the items in the list like:

for x_value in codeabbey_data:
    print(x_value)

:)

Whole block is:

# My program for codeabbey

INPUT_FILE = './input.txt' # Challenge
def read_challenge(file):
    with open(file, 'r') as my_file:
        data = []           
        for i in range(int(input_file.readline())):
            data.append(my_file.readline().split()[0])
    return data

codeabbey_data = read_challenge(INPUT_FILE)

for x_value in codeabbey_data:
    print(x_value)

see ya!

Please login and solve 5 problems to be able to post at forum