分割段落文字并计算平均字数

时间:2020-08-02 16:32:36

标签: python mapreduce

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。

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
相关问题