简体   繁体   English

字符串附加问题-Python

[英]Issues with string appending - python

I'm trying to append a string in python and the following code produces 我试图在python中附加一个字符串,下面的代码产生

    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/ 我期望字符串的格式为:加工图像/ 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. 它可能是\\r或其他特殊的空白字符。 Just clean up buildVersion of all such whitespace before executing 只需在执行之前清理所有此类空白的buildVersion

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 您可以通过几种方式来执行清理任务-例如,如果buildVersion中的有效字符只是“单词字符”(字母,数字,下划线),则类似

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. 当然,很难确切地知道不知道您需要在buildVersion接受哪些字符。

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: 它可能与实际问题无关,但是,除了Alex Martelli的答案之外,我还将检查buildVersion是否曾经存在过,因为否则此处发布的所有解决方案都会给您带来另一个错误:

import re

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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