你的问题不是很清楚。如果你想去掉逗号后的任何部分(正如你的文字所暗示的那样),那么一个相当易读的单行应该做:
cleaned_line = " ".join([field.split(",")[0] for field in line.split()])
如果要将包含逗号分隔字段的行扩展为多行(如您的示例所示),则应使用该itertools.product
函数:
import itertools
line = "a b c d,e,f g h i,j,k,l m n"
line_fields = [field.split(",") for field in line.split()]
for expanded_line_fields in itertools.product(*line_fields):
print " ".join(expanded_line_fields)
这是输出:
a b c d g h i m n
a b c d g h j m n
a b c d g h k m n
a b c d g h l m n
a b c e g h i m n
a b c e g h j m n
a b c e g h k m n
a b c e g h l m n
a b c f g h i m n
a b c f g h j m n
a b c f g h k m n
a b c f g h l m n
如果出于某种原因保持原始间距line.split()
很重要,那么您可以替换为re.findall("([^ ]*| *)", line)
:
import re
import itertools
line = "a b c d,e,f g h i,j,k,l m n"
line_fields = [field.split(",") for field in re.findall("([^ ]+| +)", line)]
for expanded_line_fields in itertools.product(*line_fields):
print "".join(expanded_line_fields)
这是输出:
a b c d g h i m n
a b c d g h j m n
a b c d g h k m n
a b c d g h l m n
a b c e g h i m n
a b c e g h j m n
a b c e g h k m n
a b c e g h l m n
a b c f g h i m n
a b c f g h j m n
a b c f g h k m n
a b c f g h l m n