Python 迭代字符串格式化/解析?

Python 迭代字符串格式化/解析?,python,Python,我正在从文本文件中读取位置记录,例如,它如下所示: AB ATEA 000401550 每个记录都分配了特定数量的字符,例如: Code: AB (characters from 0 - 2) Name: ATEA (characters from 3 - 7) Value1: 00040 (characters from 8 - 13) Value2: 1550 (characters from 13 - 16) Code = X i.e. string Name = X i.e. str

我正在从文本文件中读取位置记录,例如,它如下所示:

AB ATEA 000401550
每个记录都分配了特定数量的字符,例如:

Code: AB (characters from 0 - 2)
Name: ATEA (characters from 3 - 7)
Value1: 00040 (characters from 8 - 13)
Value2: 1550 (characters from 13 - 16)
Code = X i.e. string
Name = X i.e. string
Value1 = 9V9(4) i.e. float with 4 decimals, i.e. 0.0040
Value2 = 9(2)V9(2) i.e. float with 2 decimals, i.e. 15.50
    def converter(name, value):
        adict = {'Code':'%s' % value,
                 'Name':'%s' % value, 
                 'Value1':float('%s.%s' % (value[:1],value[1:])),
                 'Value2':float('%s.%s' % (value[:2],value[2:]))}
        return adict[name]
使用循环和元组列表作为记录键和字符位置,并将这些记录存储在字典中,我对此进行解析没有问题:

   alist = [('Code',0,2),('Name',3,7),('Value1',8,13),('Value2',13,16)]
   adict = {}
   for x in afile:
      for a, b, c in alist:
         adict[a] = x[b:c]
现在,问题是字典值必须使用特定的数据类型和特定数量的小数进行格式化,例如:

Code: AB (characters from 0 - 2)
Name: ATEA (characters from 3 - 7)
Value1: 00040 (characters from 8 - 13)
Value2: 1550 (characters from 13 - 16)
Code = X i.e. string
Name = X i.e. string
Value1 = 9V9(4) i.e. float with 4 decimals, i.e. 0.0040
Value2 = 9(2)V9(2) i.e. float with 2 decimals, i.e. 15.50
    def converter(name, value):
        adict = {'Code':'%s' % value,
                 'Name':'%s' % value, 
                 'Value1':float('%s.%s' % (value[:1],value[1:])),
                 'Value2':float('%s.%s' % (value[:2],value[2:]))}
        return adict[name]
因此,我想我可以构建一个以记录名和记录值为输入的函数,然后,在这个函数中有一个包含记录值格式的字典,例如:

Code: AB (characters from 0 - 2)
Name: ATEA (characters from 3 - 7)
Value1: 00040 (characters from 8 - 13)
Value2: 1550 (characters from 13 - 16)
Code = X i.e. string
Name = X i.e. string
Value1 = 9V9(4) i.e. float with 4 decimals, i.e. 0.0040
Value2 = 9(2)V9(2) i.e. float with 2 decimals, i.e. 15.50
    def converter(name, value):
        adict = {'Code':'%s' % value,
                 'Name':'%s' % value, 
                 'Value1':float('%s.%s' % (value[:1],value[1:])),
                 'Value2':float('%s.%s' % (value[:2],value[2:]))}
        return adict[name]
问题是,当我按如下方式运行解析循环时:

   alist = [('Code',0,2),('Name',3,7),('Value1',8,13),('Value2',13,16)]
   adict = {}
   for x in afile:
      for a, b, c in alist:
         adict[a] = converter(a,x[b:c])

Python抛出ValueError,因为函数中的值输入在运行时通过字典中的所有项传递,因此,当“AB”传递到“float()”时,字典创建停止,Python抛出错误。

您可以为每个项指定一个转换器:

def float_converter(value):
    return float('{0}.{1}'.format(value[:1], value[1:]))

alist = [('Code'  , 0 , 2 , None),
         ('Name'  , 3 , 7 , None),
         ('Value1', 8 , 13, float_converter),
         ('Value2', 13, 16, float_converter)]

adict = {}
for x in afile:
   for name, start, stop, converter in alist:      
      value = x[start:stop]
      if converter:
          value = converter(value)
      adict[name] = value
在线查看它的工作情况:

如何:

adict = {}
for x in afile:
    adict={
        'Code':x[0:2],
        'Name':x[3:7],
        'Value1':int(x[8:13])/1e4,
        'Value2':int(x[13:16])/1e2
        }

谢谢你,马克!我在这里提前道歉。在我的示例中,我没有仔细说明浮点数并不总是有相同的小数,逗号也不总是在第一个数字之后。因此,例如,一个记录可能看起来像:030050,其中转换为30.050,而另一个可能看起来像:0125,其中转换为1.25。这是我的错误,没有正确地说明转换的细微差别,但是我喜欢你的建议,这引发了另一个想法,我将在稍后发布。这个新建议真的很聪明!!非常感谢你,马克!我在转换器函数中仅使用字符串构建字典,然后使用“try”语句返回浮点,或者在“try”引发ValueError异常时返回字符串,从而解决了这个问题。然而,我不认为这是安全的,所以我将研究实施你的建议。