value =
1|a b c d
1|b c d
2|a b b g h
我正在尝试为上述每个段落(1,2)建立一个ID,并期望得到类似1的结果:(a,b,c,d,b,c,d)
尝试使用(在Python中) id,结果= value.strip()。split(“ |”)
但是它没有说期望2个结果并得到1。
答案 0 :(得分:0)
为了管理空白行,可以使用if
语句检查是否返回了2个项目,也可以使用try/except
捕获错误并分别处理空白行
temp = value.strip().split(“|”)
if len(temp) == 2:
Id, result = temp
# do the rest of your processing here\
else:
pass # or do something for the blank line
try:
Id, result = value.strip().split(“|”)
except ValueError:
pass # or do something for the blank line