Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

string - 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. The section using fnmatch is successfully printing the matched records but not changing / parsing out the matched pattern.

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.)

The result samples are unchanged from original text 1000STR92SE 8000STR37NW 7000STR35SW 8000STR44 1000STR88SE 1000STR74SE

But results need to be 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])
question from:https://stackoverflow.com/questions/65923683/python-2-7-fnmatch-not-editing-text

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Remove the ? in the string you want to strip. It is a wildcard used in pattern matching.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...