inFile = open (arcpy.GetParameterAsText(0),'r') outFile = open (arcpy.GetParameterAsText(1),'w') buffer = [] for line in inFile:: line = line.strip() buffer = line.split(' ') buffer = filter (None, buffer) outFile.writelines(",".join(buffer)) outFile.writelines("".join('\n')) inFile.close() outFile.close()
Solved! Go to Solution.
import arcpy, os, time, glob arcpy.env.overwriteOutput = True arcpy.env.workspace = ws = arcpy.GetParameterAsText(0) SR = arcpy.GetParameterAsText(1) gdb = str(arcpy.CreateFileGDB_management(ws, 'VegetationAnalysis.gdb').getOutput(0)) csv_fold = os.path.join(ws, 'CSV') if not os.path.exists(csv_fold): os.makedirs(csv_fold) for txt in glob.glob(os.path.join(ws, '*.txt')): with open(txt) as rd: lines = [line for line in rd.readlines() if ' NG' in line] arcpy.AddMessage("Creating CSV file for %s...." %os.path.basename(txt)) outFile = os.path.join(csv_fold, os.path.basename(txt.replace('.txt','.csv'))) with open(outFile, 'w') as csv: csv.write("X,Y,Z,Station,AboveGround,AboveGroundOK,BackWeatherStrCase1,BackWeatherStrCase2,VertorRadialMargin,HorzMargin,MinDistToWireGrow,OK,RadialMargin,MinDistToWireFall,DistToWire,OK\n") csv.write('\n'.join(",".join(line.strip().split()) for line in lines)) arcpy.AddMessage("Generating Feature Class...") arcpy.MakeXYEventLayer_management(outFile,'X','Y','XY_event_layer',SR,'Z') arcpy.CopyFeatures_management('XY_event_layer',os.path.join(gdb, 'SurveyPoints_%s' %os.path.basename(txt.split('.')[0]))) arcpy.AddMessage("Complete. Files located at " + gdb)
I think the version of ArcGIS made the difference. I was using ArcGIS 10.0 and if I ran same file in ArcGIS 10.1, it didn't seem to have that problem. My field type came out as double in 10.0. I checked and figured out that only strings with 'F' in them have this problem. I can run the data in computers with ArcGIS 10.1 for now. However, I still want to know why 10.0 does this. Just curious!
And again, thank you very much for your help!
[example.csv] Format=CSVDelimited ColNameHeader=True MaxScanRows=1 Col7=BackWeatherStrCase1 Char
IDLE 2.6.5 ==== No Subprocess ==== >>> outFile = r'C:\mapdocs\temp\textINI\CSV\example.csv' >>> arcpy.MakeXYEventLayer_management(outFile,'X','Y','XY_event_layer') <Result 'XY_event_layer'> >>> arcpy.CopyFeatures_management('XY_event_layer', r'C:\mapdocs\temp\textINI\VegetationAnalysis.gdb\test1') <Result 'C:\\mapdocs\\temp\\textINI\\VegetationAnalysis.gdb\\test1'> >>> # Let's test read the file gdb results: >>> rows = arcpy.SearchCursor(r'C:\mapdocs\temp\textINI\VegetationAnalysis.gdb\test1') >>> # ...get the 1st record >>> row = rows.next() >>> # ...get a field list to call by name with getValue: >>> fields = arcpy.ListFields(r'C:\mapdocs\temp\textINI\VegetationAnalysis.gdb\test1') >>> # For the 1st row, print the field values read via the cursor: >>> for field in fields: print field.name, '...', row.getValue(field.name) OBJECTID ... 1 Shape ... <geoprocessing describe geometry object object at 0x096D9050> X ... 751274.78 Y ... 1484273.55 Z ... 20.82 Station ... 270.52 AboveGround ... 16.97 AboveGroundOK ... OK BackWeatherStrCase1 ... None # ah, this is the Null value, of course incorrect BackWeatherStrCase2 ... WCD-0in-0psf-100degC-WCD VertorRadialMargin ... -0.01 HorzMargin ... 0.0 MinDistToWireGrow ... 9.99 OK ... NG RadialMargin ... 0.0 MinDistToWireFall ... 0.0 DistToWire ... 9.99 OK_1 ... >>> >>> row.Station 270.51999999999998 >>> # what is the BackWeatherStrCase1 88F4 value read as? >>> while row: if row.Station > 300.0: print row.BackWeatherStrCase1 break row = rows.next() 884.0 # clearly wrong! >>>
>>> print outFile C:\mapdocs\temp\textINI\CSV\example.csv >>> arcpy.MakeXYEventLayer_management(outFile,'X','Y','XY_event_layer') <Result 'XY_event_layer'> >>> arcpy.CopyFeatures_management('XY_event_layer', r'C:\mapdocs\temp\textINI\VegetationAnalysis.gdb\test1') <Result 'C:\\mapdocs\\temp\\textINI\\VegetationAnalysis.gdb\\test1'> >>> rows = arcpy.SearchCursor(r'C:\mapdocs\temp\textINI\VegetationAnalysis.gdb\test1') >>> # this time, just printing all the vals in the BackWeatherStrCase1 field: >>> for row in rows: print row.BackWeatherStrCase1 COUSOU COUSOU COUSOU COUSOU COUSOU 88F4 88F4 88F4 88F6 88F10 88F10 88F10 88F10 88F10 88F15 88F15 88F15 88F15 88F15 88F15 88F15 88F15 88F15 88F15 88F15 88F16 88F16 88F16 >>> # read error corrected.