简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM