[Solved] How to read words seperated by spaces as a single value


Without seeing actual code, it’s difficult to know exactly what’s gone wrong here.

readlines will split a file on a newline delimiter. For complete cross-platform compatibility, open files in the “universal” compatibility mode (PEP-278 https://www.python.org/dev/peps/pep-0278/), which avoids questions about whether your lines end in ‘\n’, ‘\r\n’, or some other variation (depends on whether you’re on a DOS or Unix-like system):

with open('input.txt', 'rU') as t:
    lines = t.readlines()

You can then take the set of lines and, given that your input uses a tab delimiter, split each line into a key/value pair:

results = list()
for line in lines:
    key,value = line.strip().split('\t')
    results.append(key)
    results.append(value)

There are ways to collapse this down into a list comprehension, but the above is a very explicit example of how to achieve your desired result.

solved How to read words seperated by spaces as a single value