get first three digits get next nine digits if first three digits == '120' get next four digits # and so on...
>>> "a\n".rstrip() 'a'
testtxt = open('D:/Python_Tests/test.txt', 'r') testlist = testtxt.readlines() testtxt.close() dict_ = {} for item in testlist: RECORDTYPE = item[0:3] item_ = item.strip('\n')) try: dict_[RECORDTYPE].append(item_[3:]) except KeyError: dict_[RECORDTYPE] = [item_[3:]] for key in dict_: textout = open('D:/Python_Tests/textout_%s.txt' %key, 'w') for item in dict_[key]: if key == '100': textout.write('100'+','+item[:9]+','+item[9:10]+','+item[10:14]+','+item[14:18]+'\n') elif key == '110': textout.write('110'+','+item[:9]+','+item[9:18]+','+item[18:35]+'\n') elif key == '120': textout.write('120'+','+item[:9]+','+item[13:613]+','+'\n') textout.close()
testtxt = open('D:/Python_Tests/test.txt', 'r') testlist = testtxt.readlines() testtxt.close() dict_ = {} def chomp(s): return s[:-1] if s.endswith('\n') else s #Keeps trailing whitespace for item in testlist: RECORDTYPE = item[0:3] item_ = chomp(item) #used in place of xyz.strip('\n') try: dict_[RECORDTYPE].append(item_[3:]) except KeyError: dict_[RECORDTYPE] = [item_[3:]] for key in dict_: textout = open('D:/Python_Tests/textout_%s.txt' %key, 'w') for item in dict_[key]: if key == '100': textout.write('100'+','+item[:9]+','+item[9:15]+','+item[15:18]+','+item[18:]+'\n') elif key == '110': textout.write('110'+','+item[:9]+','+item[9:12]+','+item[12:17]+','+item[17:]+'\n') elif key == '120': textout.write('120'+','+item[:9]+','+item[9:15]+','+item[15:]+'\n') textout.close()
12012345678ASOMEOTHERSTUFF
file: 120 # parcelnum, text 12345678A,SOMEOTHERSTUFF
file: 120 # parcelnum, text 1234567,8ASOMEOTHERSTUFF
#test.txt= #10012345678ABCDEF123abc #11012345678ABC12345abcd #12012345678111111abcdef #10012345678AABCDEF123abc #11012345678AABC12345abcd #12012345678A111111abcdef testtxt = open('D:/Python_Tests/test.txt', 'r') testlist = testtxt.readlines() testtxt.close() dict_ = {} for item in testlist: RECORDTYPE = item[0:3] item_ = item.strip('\n') try: dict_[RECORDTYPE].append(item_[3:]) except KeyError: dict_[RECORDTYPE] = [item_[3:]] for key in dict_: textout = open('D:/Python_Tests/textout_%s.txt' %key, 'w') for item in dict_[key]: if key == '100': textout.write('100'+','+item[:9]+','+item[9:18]+','+item[18:21]+','+item[21:23]+'\n')#I added the if and elif to format each record type separately elif key == '110': textout.write('110'+','+item[:9]+','+item[9:12]+','+item[12:18]+'\n') elif key == '120': textout.write('120'+','+item[:9]+','+item[9:12]+','+item[12:]+'\n') textout.close()
for key in dict_: textout = open('D:/Python_Tests/textout_%s.txt' %key, 'w') for item in dict_[key]: if key == '100': textout.write('100'+','+item.ljust(5)+'n'+'\n') elif key == '110': textout.write('100'+','+item[:9]+','+item[9:18]+','+item[18:21]+','+item[21:23]+'\n') elif key == '120': textout.write('100'+','+item[:9]+','+item[9:18]+','+item[18:21]+','+item[21:23]+'\n') textout.close()
'120' # record type - the 'key' L ['12345678AFIXEDWIDTH', '12345678FIXEDWIDTH'] # other info, stored within a list '110' L etc., etc.
# Read mode opens a file for reading only. DataFileIn = open("input.txt", "r") # Read all the lines into a list. DataList = DataFileIn.readlines() DataFileIn.close() DataDict = {} for item in DataList: # iterate over the rows - each item is the string of data RECORDTYPE = item[0:3] # get parts 0 to 3 of the string (first 3 digits) item_ = item.strip('\n') # get rid of new line characters at the ends (if they are there - does nothing if not) try: DataDict[RECORDTYPE].append(item_[3:]) # try to append the rest to the dictionary sub-list as a list item except KeyError: DataDict[RECORDTYPE] = [item_[3:]] # if this is the first time this record has appeared, add it as a list item for key in DataDict: # for every record type DataTextOut = open('output_%s.txt' % key, 'w') # i.e. output_120.txt for item in DataDict[key]: # for each line in the list DataTextOut.write(item+'\n') # write the data, then add a new line DataTextOut.close() # close this particular file
''' ParsingDataDemo.py A demo file to parse data which is quasi-fixed width File and script must reside in the same folder...fix this if you want ''' import sys, os data_path = (os.path.dirname(sys.argv[0]) + "/").replace("\\","/") #can be skipped if you follow data_file = data_path + "ParsingDataDemoData.txt" #fix this or better still create a tool #data_file should be sys.argv[1] which allows a user to select a file in a folder a_file = open(data_file) data = a_file.readlines() for a_line in data: record_type = a_line[:3] parcel_num = a_line[3:11] the_rest = a_line[11:] print record_type, parcel_num, the_rest
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.