Python Forum
Read directly from excel file using python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read directly from excel file using python script
#1
So I have iterated over a text file to create a dataset with feature classes in a new gdb. I am having trouble trying to figure out how to modify the script to do this reading directly off an excel file. I would convert it but I have to do it directly. My spreadsheet has 3 worksheets but I need it to work if more are added later also.
import arcpy, fileinput, os
arcpy.env.workspace = "C:/path"
folder = "C/path"
database = "GPS_Results.gdb"
fdataset = "Study_Areas"
spatialref = arcpy.SpatialReference("NAD 1983 UTM Zone 12N")
arcpy.CreateFileGDB_management(folder + "/", database)
arcpy.CreateFeatureDataset_management(os.path.join(folder, database), fdataset, spatialref)
for textfile in arcpy.ListFiles("*.txt"):
    newfc = os.path.splitext(textfile)[0]
    outpath = os.path.join(folder, database, fdataset)
    arcpy.CreateFeatureclass_management(outpath, newfc, "POINT")
    cursor = arcpy.da.InsertCursor(os.path.join(outpath, newfc), ["SHAPE@"])
    point = arcpy.Point()
    infile = os.path.join(folder, textfile)
    skipline = 0
    for line in fileinput.input(infile):
        if skipline != 0:
            point.ID, point.X, point.Y = line.split( )
            cursor.insertRow([point])
        else:
            skipline += 1
    del cursor
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to convert .trc/.txt file into excel using python ebola 3 2,016 Jan-15-2023, 10:37 PM
Last Post: Yoriz
  Re-write BASH script to Python script popi75 5 2,445 Apr-30-2021, 03:52 PM
Last Post: metulburr
  Excel File reading vanjoe198 1 2,038 Mar-31-2021, 11:53 AM
Last Post: snippsat
  about write file wrong (Edit directly online) CNenfan 4 2,519 Jan-29-2021, 05:32 AM
Last Post: deanhystad
  Can we store value in file if we open file in read mode? prasanthbab1234 3 2,574 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  [split] how to read a specific row in CSV file ? laxmipython 2 8,878 May-22-2020, 12:19 PM
Last Post: Larz60+
  Read data from a CSV file in S3 bucket and store it in a dictionary in python Rupini 3 6,996 May-15-2020, 04:57 PM
Last Post: snippsat
  read from file mcgrim 16 6,155 May-14-2019, 10:31 AM
Last Post: mcgrim
  Read a data from text file (Notepad) Leonzxd 24 13,904 May-23-2018, 12:17 AM
Last Post: wavic
  Homework - Read from/Write to file (renamed from Help help help) Amitkafle 1 3,045 Jan-11-2018, 07:24 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020