简体   繁体   English

拆分用户输入并在Python字符串中添加字符

[英]Split user input and add character in Python string

I have a Python script that accepts user input (it is scanned with a barcode) that subsequently generates a CSV file with all of the user's data. 我有一个Python脚本,它接受用户输入(使用条形码进行扫描),随后会生成包含所有用户数据的CSV文件。 This CSV file is then used to drive a Crystal Report. 然后,此CSV文件用于驱动Crystal报表。

The input consists of a single string of concatenated fields delimited by a space- Item #, Lot #, and Quantity. 输入由连接的字符串的单个字符串组成,这些字符串由空格分隔,即项目编号,批号和数量。

    Example: G00177 LOT12345 24

The problem, however, is that the Item # OR Lot # could potentially be numeric instead of a string: 但是,问题在于项目号或批号可能是数字而不是字符串:

    Example 1: G00177 12345 24
    Example 2: 00177 12345 24

My thought was to append a character to the beginning of the Item # and Lot #, forcing it into a string, and then using Crystal to automatically suppress the added character. 我的想法是在Item#和Lot#的开头附加一个字符,将其强制为字符串,然后使用Crystal自动抑制添加的字符。

So far, I have only been able to add a character to the beginning of the Item #- not the Lot #. 到目前为止,我只能在商品#的开头添加一个字符,而不能在批号中添加一个字符。 I also do not want to append to the Quantity field as it will always be numeric. 我也不想附加到“数量”字段,因为它将始终是数字。

Any criticism, insight, or suggestions are greatly appreciated. 任何批评,见解或建议,我们将不胜感激。

I am just learning with Python and I know my approach is not the best. 我只是在学习Python,我知道我的方法不是最好的。

Thank you for your time. 感谢您的时间。

You can try something like this: 您可以尝试如下操作:

report_one = '00177 12345 24'
report_two = 'G0177 12345 24'
report_three = 'G0177 I2345 24'

def convert(original):
    s = original.split()
    for i in range(2):       # 2 is used here because that's how many 'words'
        try:                 #     we want to check for numeric-ness. This can
            int(s[i])        #     be adjusted if you have to change your schema
        except ValueError:   #     at some point in the future.
            s[i] += 'S'
    return ' '.join(s)

print convert(report_one)    # Prints 00177 12345 24
print convert(report_two)    # Prints G0177S 12345 24
print convert(report_three)  # Prints G0177S I2345S 24

This first splits up the string into its individual words. 这首先将字符串拆分成各个单词。 You know that the first and the second ones are potentially integers, and you need a way for the next system to understand them as strings. 您知道第一个和第二个可能是整数,并且您需要让下一个系统将它们理解为字符串的方式。

Using the split words, doing a cast on the word into an int under a try block will throw a ValueError if the string is not an integer. 如果字符串不是整数,则使用拆分词在try块下将该词转换为int会抛出ValueError That is, if it contains any letters, the except ValueError clause will kick in. At this point, you can simply append a letter to the end, and have your next system scan the word for that ending letter (here my example uses an 'S' ). 也就是说,如果它包含任何字母,则将启动except ValueError子句。此时,您可以简单地将字母附加到末尾,然后让您的下一个系统扫描该末尾字母的单词(此处我的示例使用'S' )。

If you want to append a letter to the beginning, you can replace s[i] += 'S' with: s[i] = 'S' + s[i] 如果要在开头添加字母,则可以将s[i] += 'S'替换为: s[i] = 'S' + s[i]

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

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