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>EDIT: this answer assumes a "raw" format. It won't work here. It <em>would</em> work if <code>readlines[1]</code> was already a list of strings obtained by splitting a line like <code>"1 4 5 6 7"</code>, which isn't the case here since line contains a python list written as a <code>str(list)</code>. <code>ast.literal.eval</code> is the right choice in that case, <em>if</em> the input format is satisfying and not a mistake in the first place.</p>

<p>when you do:</p>

<pre><code>positionslist = readlines[1]
</code></pre>

<p>there's no way you get something other than a list of strings containing integers. You have to convert them for example like this:</p>

<pre><code>positionslist = [int(x) for x in readlines[1]]
</code></pre>

<p>In your hardcoded example, you're using integers directly and it works.</p>

<p>Note: as cricket_007 suggested, since you just iterate on <code>positionslist</code>, you can use</p>

<pre><code>positionslist = map(int,readlines[1])
</code></pre>

<p>It applies the <code>int</code> function to each item of <code>readlines[1]</code>, returns an iterable (returns <code>list</code> in Python 2, so not that much different from the list comprehension above in that case).</p>

<p>In Python 3, that avoids to create/allocate a list that you don't need since you don't use indexes (if you want to debug it you can't since you can iterate only once on it), so it is more performant.</p>

<p>of course, if some line is not a digit it will crash with an explicit error <code>ValueError: invalid literal for int() with base 10</code></p>

python