无法将字符串与“/”字符进行比较

时间:2013-07-28 20:59:17

标签: python string list

我有一个二维列表列表,我需要根据第一个记录字段进行搜索,内容是包含正斜杠字符的字符串。

测试arg_size时,如果存在正斜杠字符,则搜索始终会失败。我已对参数进行了硬编码以便进行故障排除。

提前致谢。

# -*- coding: UTF-8 -*-
bolt = {}    # create empty dictionary
thread_geometry = {}    # create empty dictionary

#  variables for record position
size = 0
dia_in = 1
dia_mm = 2
tpi_UNC = 3
pitch_UNC_in = 4
pitch_UNC_mm = 5
tpi_UNF = 6
pitch_UNF_in = 7
pitch_UNF_mm = 8
tpi_UNEF = 9
pitch_UNEF_in = 10
pitch_UNEF_mm = 11

# lookup table for bolt standards,negative values represents NULL
bolt_table = [
        [r"#0", 0.06, 1.524, -1, -1, -1, 80, 0.0125, 0.3175, -1, -1, -1 ],
        [r"#1", 0.073, 1.8542, 64, 0.015625, 0.3969, 72, 0.013888, 0.3528, -1, -1, -1 ],
        [r"#2", 0.086, 2.1844, 56, 0.017857, 0.4536, 64, 0.015625, 0.3969, -1, -1, -1 ],
        [r"#3", 0.099, 2.5146, 48, 0.020833, 0.5292, 56, 0.017857, 0.4536, -1, -1, -1 ],
        [r"#4", 0.112, 2.8448, 40, 0.025, 0.635, 48, 0.020833, 0.5292, -1, -1, -1 ],
        [r"#5", 0.125, 3.175, 40, 0.025, 0.635, 44, 0.022727, 0.5773, -1, -1, -1 ],
        [r"#6", 0.138, 3.5052, 32, 0.03125, 0.7938, 40, 0.025, 0.635, -1, -1, -1 ],
        [r"#8", 0.164, 4.1656, 32, 0.03125, 0.7938, 36, 0.027778, 0.7056, -1, -1, -1 ],
        [r"#10", 0.19, 4.826, 24, 0.041667, 1.0583, 32, 0.03125, 0.7938, -1, -1, -1 ],
        [r"#12", 0.216, 5.4864, 24, 0.041667, 1.0583, 28, 0.035714, 0.9071, 32, 0.03125, 0.7938 ],
        [r"1/4", 0.25, 6.35, 20, 0.05, 1.27, 28, 0.035714, 0.9071, 32, 0.03125, 0.7938 ],
        [r"5⁄16", 0.3125, 7.9375, 18, 0.055556, 1.4111, 24, 0.041667, 1.0583, 32, 0.03125, 0.7938 ],
        [ur"3⁄8", 0.375, 9.525, 16, 0.0625, 1.5875, 24, 0.041667, 1.0583, 32, 0.03125, 0.7938 ],
        [r"7⁄16", 0.4375, 11.1125, 14, 0.071428, 1.8143, 20, 0.05, 1.27, 28, 0.035714, 0.9071 ],
        [r"1⁄2", 0.5, 12.7, 13, 0.076923, 1.9538, 20, 0.05, 1.27, 28, 0.035714, 0.9071 ],
        [r"9⁄16", 0.5625, 14.2875, 12, 0.083333, 2.1167, 18, 0.055556, 1.4111, 24, 0.041667, 1.0583 ],
        [r"5⁄8", 0.625, 15.875, 11, 0.090909, 2.3091, 18, 0.055556, 1.4111, 24, 0.041667, 1.0583 ],
        [r"3⁄4", 0.75, 19.05, 10, 0.1, 2.54, 16, 0.0625, 1.5875, 20, 0.05, 1.27 ],
        [r"7⁄8", 0.875, 22.225, 9, 0.111111, 2.8222, 14, 0.071428, 1.8143, 20, 0.05, 1.27 ],
        [r"1", 1, 25.4, 8, 0.125, 3.175, 12, 0.083333, 2.1167, 20, 0.05, 1.27 ],
        ]



# # # # ------------------- Main -------------------------------------------- # # # #


#  Get bolt parameters
#  if the no value is entered the value will be NULL or set to a default

arg_unit = 'mm'
if arg_unit.lower() == 'mm':
    bolt['unit'] = 'mm'
else:
    bolt['unit'] = 'in'

arg_size = '3/8'
for x in bolt_table:
    if str(x[size]) == str(arg_size):
        bolt['size'] = str(arg_size)

arg_length = 2.5
if arg_length > 0:
    bolt['length'] = arg_length
else:
    sys.stderr.write(" Bolt length is a required field!!!")
    sys.exit()

arg_thread = 'UNC'
if arg_thread.upper() == 'UNC' or arg_thread.upper() == 'UNF' or arg_thread.upper() == 'UNEF':
    bolt['thread'] = arg_thread.upper()

print bolt.viewitems()

1 个答案:

答案 0 :(得分:1)

键盘上的斜线与桌子上的斜线不一样。有不同的符号。 修改表以将unicode斜杠替换为ascii斜杠或使用How to read Unicode input and compare Unicode strings in Python?中的规范化解决方案

我认为您从网上复制粘贴表的值