1

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

1
  • buildVersion.rstrip() Commented Feb 21, 2015 at 23:03

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.