TypeError: unsupported operand type(s) for -: 'str' and 'int'
<p>Right now my code will create a text file including the positions of each word in a sentence in a list, and each word in a list - like so: </p>

<p><a href="http://i.stack.imgur.com/UeqOs.png" rel="nofollow"><img src="http://i.stack.imgur.com/UeqOs.png" alt="file content"></a></p>

<p>My code then opens back up the file and should be able to recreate the sentence using those positions and the words in the text file, but it doesn't seem to work with with the readlines code for some reason.</p>

<pre><code>openfilename = open(openfilename,"r")
readlines = openfilename.readlines()

wordslist = readlines[0]
positionslist = readlines[1]

print(positionslist)
print(wordslist)

originalsentence = " ".join([wordslist[x-1] for x in positionslist])
print(originalsentence)
</code></pre>

<p>For example this comes up with wordslist and positionslist coming out as:</p>

<pre><code>[1, 2, 3, 2]
['test123', ',', '.']
</code></pre>

<p>Where as if I were to use:</p>

<pre><code>positionslist = [1, 2, 3, 2]
wordslist = ['test123', ',', '.']
originalsentence = " ".join([wordslist[x-1] for x in positionslist])
print(originalsentence)
</code></pre>

<p>It would work, and I have no idea why because, as a newcomer to python, you'd think they'd work the same. Looking at other people's post with the same error I'm supposedly missing a </p>

<pre><code>(int(...)
</code></pre>

<p>line of code somewhere but I'm not sure where, or if that's even the problem.</p>

<p>Another solution is to use <a href="https://docs.python.org/3/library/ast.html#ast.literal_eval" rel="nofollow"><code>ast.literal_eval()</code></a> to convert your string <code>u'[1, 2, 3, 2]</code> directly into a list of interger:</p>

<pre><code>import ast

with open(openfilename, "r") as f:
    positionslist = ast.literal_eval(f.readline())
    wordslist = ast.literal_eval(f.readline())

    print(" ".join([wordslist[x-1] for x in positionslist]))
</code></pre>

<p>Also, the <a href="https://docs.python.org/3/reference/compound_stmts.html#the-with-statement" rel="nofollow"><code>with</code></a> statement replace a try and catch. It also automatically close the file after the block.</p>

python