如何从Python中删除CSV行中的POS标签和其他字符?

时间:2015-04-07 15:42:32

标签: python python-2.7

如何从CSV文件中读取的字符串中删除逗号,括号,括号和POS标记?

示例输入字符串:

'[("it", "P"), ("was", "VP"), ("sleeping", "N")]' , ' [("is", "P"), ("it", "VP"), ("correct", "N")] '

示例输出:

  

它正在睡觉,是不是正确

1 个答案:

答案 0 :(得分:0)

您可以使用csv.reader的参数控制csv格式:

import csv

data = csv.reader(<file>, quotechar='\'', skipinitialspace=True)
print([" ".join([e[0] for e in eval(field.strip())]) for row in data for field in row])

输出:

['it was sleeping', 'is it correct']