简体   繁体   English

Python 2.7 fnmatch 不编辑文本

[英]Python 2.7 fnmatch NOT editing text

I have a file with 600k+ records of string labels I am trying to edit with an update cursor using both the string modules and fnmatch to find patterns to edit.我有一个包含 600k+ 字符串标签记录的文件,我正在尝试使用字符串模块和 fnmatch 来查找要编辑的模式,并使用更新 cursor 进行编辑。 The section using fnmatch is successfully printing the matched records but not changing / parsing out the matched pattern.使用 fnmatch 的部分成功打印了匹配的记录,但没有更改/解析出匹配的模式。

There is no error and there is no change to the record.没有错误,记录也没有变化。 What am I missing with my syntax?我的语法缺少什么?

(Also - side issue, the last print row 4 statement only prints the first record of the file.) (另外 - 附带问题,最后一个打印行 4 语句仅打印文件的第一条记录。)

The result samples are unchanged from original text 1000STR92SE 8000STR37NW 7000STR35SW 8000STR44 1000STR88SE 1000STR74SE结果样本与原文无变化 1000STR92SE 8000STR37NW 7000STR35SW 8000STR44 1000STR88SE 1000STR74SE

But results need to be 92SE 37NW 35SW 44 88SE 74SE但结果需要是 92SE 37NW 35SW 44 88SE 74SE

def newlabel():
#Global Conversions - works on editing the string
    with arcpy.da.UpdateCursor(mvum_fc, newfields) as uc:
        for row in uc:
            if row[1] == None or row[1].startswith('0'):
                row[4] = '{}'.format(row[4].lstrip(ascii_letters).replace('_', "-").lstrip('0').replace(' ', '-'))

            if row[1] == None or row[1].startswith('-'):
                row[4] = '{}'.format(row[4].lstrip('-').lstrip('0'))
#Regional Conversions - lstrip and replace tried - not changing text.
            elif row[1].startswith('0118'):
                pattern = ('?000STR*')
                match = fnmatch.fnmatch(row[4], pattern)
                if match == True:
                    row[4] = '{}'.format(row[4].lstrip('?000STR'))
                    print(row[4])
            uc.updateRow(row)
            print(row[4])

Remove the ?删除? in the string you want to strip.在你想要剥离的字符串中。 It is a wildcard used in pattern matching.它是用于模式匹配的通配符。

row[4] = '{}'.format(row[4].lstrip('000STR'))

I have resolved this by avoiding the.lstrip:我通过避免使用 .lstrip 解决了这个问题:

row[4] = '{}'.format(row[4].lstrip('000STR')) row[4] = '{}'.format(row[4].lstrip('000STR'))

and simply replacing it with:并简单地将其替换为:

row[4] = '{}'.format(row[4][7:]) row[4] = '{}'.format(row[4][7:])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM