Siddharthan Asokan
Siddharthan Asokan

Reputation: 4441

Issues with string appending - python

I'm trying to append a string in python and the following code produces

    buildVersion =request.values.get("buildVersion", None)
    pathToSave = 'processedImages/%s/'%buildVersion
    print pathToSave

prints out

   processedImages/V41
   /

I'm expecting the string to be of format: processedImages/V41/

It doesn't seem to be a new line character.

 pathToSave = pathToSave.replace("\n", "")

This dint really help

Upvotes: 1

Views: 97

Answers (2)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52103

It might not be relevant to actual question but, in addition to Alex Martelli's answer, I would also check if buildVersion ever exists in the first place, because otherwise all solutions posted here will give you another errors:

import re

buildVersion = request.values.get('buildVersion')
if buildVersion is not None:
    return 'processedImages/{}/'.format(re.sub('\W+', '', buildVersion))
else:
    return None

Upvotes: 1

Alex Martelli
Alex Martelli

Reputation: 881555

It might be a \r or other special whitespace character. Just clean up buildVersion of all such whitespace before executing

pathToSave = 'processedImages/%s/' % buildVersion

You can approach the clean-up task in several ways -- for example, if valid characters in buildVersion are only "word characters" (letters, digits, underscore), something like

import re
buildVersion = re.sub('\W+', '', buildVersion)

would usefully clean up even whitespace inside the string. It's hard to be more specific without knowing exactly what characters you need to accept in buildVersion, of course.

Upvotes: 1

Related Questions