2 回答
TA贡献1845条经验 获得超8个赞
您可以让“正常”拆分来划分行
>>> s = """\
... FACILITIES \ PARK
... ROADS \ GRASS/TURF BLVD MAINTENANCE
... ROADS \ SIDEWALKS \ REPLACEMENT
... FACILITIES \ PARKING - MAIN ST
... RECREATION \ BANDSHELL \ PROPERTY \ BUILDING-GENERAL
... FACILITIES \ FIREHALL \ PLUMBING
... FACILITIES
... """
>>> for row in s.splitlines():
... print(row.split(" \\ "))
...
['FACILITIES', 'PARK']
['ROADS', 'GRASS/TURF BLVD MAINTENANCE']
['ROADS', 'SIDEWALKS', 'REPLACEMENT']
['FACILITIES', 'PARKING - MAIN ST']
['RECREATION', 'BANDSHELL', 'PROPERTY', 'BUILDING-GENERAL']
['FACILITIES', 'FIREHALL', 'PLUMBING']
['FACILITIES']
然后您可以迭代返回的列表,设置每个值
max_col_length = 10 # to be updated by author
for row in s.splitlines():
for index, atom in enumerate(row.split(" \\ "), 1): # count from 1
mbo = "create a new row" # not shown by author
mbo.setValue("WOEQ{}".format(index), atom[:max_col_length])
"INSERT row if processing rows individually"
"INSERT all rows if able to upload them all at the same time"
如果您需要始终在列表中提供至少 4 个(或 N 个)成员,您可以使用itertools.repeat填写剩余值
>>> import itertools
>>> mylist = ['RECREATION', 'BANDSHELL', 'PROPERTY']
>>> mylist.extend(list(itertools.repeat(None, 4 - len(mylist))))
>>> print(mylist)
['RECREATION', 'BANDSHELL', 'PROPERTY', None]
TA贡献1872条经验 获得超3个赞
这是我最终使用的脚本(它是 @ti7 答案的变体)。
在新的层次结构路径少于四个级别的情况下,脚本会将现有值清空。
parts = s.split(r' \ ')
for i, part in enumerate(parts, 1):
mbo.setValue(col_prefix + str(i), part)
for i in range(len(parts)+1, 5):
mbo.setValueNull(col_prefix + str(i))
添加回答
举报