Capitalize each word in a string variable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | * Encoding: UTF-8. * Convert first letter of each word to Uppercase, python version. * Capitalize each word in a string variable. * Anton Balabanov, 2017-09-14. DATA LIST FIXED /address 1-50 (A). BEGIN DATA. 248 n. delphia ave. 9273 Fairvew dr. apt 410 6324 Meadowview corner of south & maine oneWord --- не латиница END DATA. * Option 1, with title() function: super easy to use but affects camelStyle words. BEGIN PROGRAM Python. |
22 23 24 25 26 27 28 29 30 31 | import spssdata # The following 3 lines are overheads for export data to python and create new address variable data = spssdata.Spssdata(indexes=('address'), accessType='w') data.append(spssdata.vdef('NewAddress', vfmt=['A', 50])) data.commitdict() for i,row in enumerate(data): data.casevalues([row.address.title()]) data.CClose() |
32 33 34 35 | END PROGRAM. * Option 2, safe with camelStyle words but more verbose. BEGIN PROGRAM Python. |
36 37 38 39 40 41 42 43 44 45 46 47 | import spssdata data = spssdata.Spssdata(indexes=('address'), accessType='w') data.append(spssdata.vdef('NewAddress', vfmt=['A', 50])) data.commitdict() for i,row in enumerate(data): addr_parts = row.address.strip().split(' ') #for addr_part in addr_parts: # addr_part[0] = addr_part[0].upper() new_address = " ".join([addr_part.replace(addr_part[0], addr_part[0].upper(), 1) for addr_part in addr_parts if addr_part]) data.casevalues([new_address]) data.CClose() |
48 | END PROGRAM.
|