Hi Ted, I haven't used the r+b flag, i shall give that one a whirl at some point. I have always read original.txt and write to new.txt followed by using os.remove() cleanup if there are intermediate steps ot text file i do not want to hang onto.but i have smooshed a bit of text into csv. And i have always just passed the replaces back to the variable. e.g.data = txtFile.readline()while data: data = data.replace("/ ", "_").replace("/", "_").replace(" ", "_") outFile.write(data) data = txtFile.readline()i think it might be the r+ flag might be the sticking point. i should go read the notes on it.
I actually have tried both:txtFile.write (change)#txtFile.writelines(change)inside the with statementThe file is not getting updated.So basically, I am doing:with open (r"C:\1GIS\Sales\ReplaceString\Sales2011.txt", "r+b") as txtFile: data = txtFile.readline() print "*" * 530 print data change = data.replace("/ ", "_").replace("/", "_").replace(" ", "_") print change txtFile.write (change) #txtFile.writelines(change)
with open (r"C:\1GIS\Sales\ReplaceString\Sales2011.txt", "r+b") as txtFile: data = txtFile.readline() print "*" * 530 print data change = data.replace("/ ", "_").replace("/", "_").replace(" ", "_") print change txtFile.write (change) #txtFile.writelines(change)
Ohhhh, you're not actually doing the .write() call, you're just reading. You may want to read from the file and output to a new one to be safe.
r+ is for appending, how does rb work?
with open (r"C:\1GIS\Sales\ReplaceString\Sales2011.txt", "r+") as txtFile: data = txtFile.readline() print "*" * 530 print data change = data.replace("/ ", "_").replace("/", "_").replace(" ", "_") print change
Yes.Just to confirm myself, I created a little text file that contained this:a,b,c,d1,2,3,S4,5,6,TI used that as input to TableToTable and it spit out a table of 2 rows with 5 fields (OBJECTID, a, b, c, d)-Dave
That was fast, so txt file can be read as a table, thats interesting, something to explore for sure. thanks David. I like the last idea at least for testing purposes at 10.1. So, regarding the structure, do the tools, handle comma delimited, and treat the top row as the column headings?
Hi Ted Could depend on how your .txt files are structured, but a .txt can be read by most tools that accept a table or table view as input. So you could feed it directly into tools like Make XY Event Layer or XY To Line. Or you could read in the file in Python and write out in your own flavor with a cursor.-Dave
This may seem trivial to some, but what is the best way to go for conversion purposes, from a text file into a GIS format. I was looking to use win32com and convert a txt file right into Access, but Esri does not support the .accdb format, so this does not solve my issue, unless win32com allows to go straight into a mdb. I was also thinking of the dbf format (Seems like a dated approach), or does it make sense to just use the insertcursor from a text file right into a file GB. I get a lot of txt files from management, and I am looking to just build a tool for myself or them to get something a bit more useful and streamline my own workflow. i don't really want to go into SQL Server or Postgress for simple operations.
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.