Python copy and rename many small csv files based on selected characters within the files -
i'm not programmer; i'm pilot has done little bit of scripting in past life, i'm non-current @ this. have searched forum , found similar problems that, more expertise , time might able adapt problem, hope can closer asking own question. hope problem unique enough considering answering not feel time wasted, considering disadvantage. anyway here problem:
some of crew members periodically have need rename few hundred more 1,000 small csv files based on specific convention applied contents. not of files used in given project, subset of them used, automation makes lot of sense here. done manually needed. can move these files single directory processing, since file names unique received.
here representative excerpts 2 example csv files, preceded respective file names (as receive them):
a_13lsat_2014-04-23_1431.csv:
1,kdal curlo rw13l sat 20140414_0644,sid,n/a,ddi 2,*,rw13l(aer),sat 3,rw13l(aer),+325123.36,-0965121.20,rw31r(der),+325031.35,-0965020.95 4,1,1.2,+325123.36,-0965121.20,0.0,+325031.35,-0965020.95,2.0 3,rw31r(der),+325031.35,-0965020.95,gh13l,+324947.23,-0964929.84 4,1,2.4,+325031.35,-0965020.95,0.0,+324947.23,-0964929.84,2.0 5,ttt,0,0 5,cve,0,0
a_rrosee_2014-04-03_1419.csv:
1,kdfw seevr star rrony seevr 20140403_1340,star,n/a,ddi 2,*,rrony,seevr 3,rrony,+333455.16,-0952530.56,rowze,+333233.02,-0954016.52 4,1,12.6,+333455.16,-0952530.56,0.0,+333233.02,-0954016.52,2.0 5,eic,0,1 5,slr,0,0
i know these files not code, entered them indented in post display properly.
the files must renamed due 8.3 limitation of platform used on. convention is:
•on first line, first 2 characters in second word of second "cell" (which 6th , 7th characters of second cell), and,
•on line 2, first 3 characters of third cell, and
•the first 3 characters of fourth cell.
the contents , format of files must remain unaltered. in theory convention yields unique names every file duplication of file names should not problem.
the files above copied , renamed respectively to:
curw1sat.csv
serrosee.csv
that's it. script scan directory full of these csv files, , create renamed copies in same directory according the convention described, based on contents. i'm attempting use activestate python 2.7.7.
thanks in advance consideration.
it's not you'd call pretty, neither i; , works (and it's simple)
import os import glob fileset = set(glob.glob(os.path.basename(os.path.join(".", "*.csv")))) filename in fileset: open(filename, "r") f: csv_file = f.readlines() out = csv_file[0].split(",")[1].split(" ")[1][:2] out += csv_file[1].split(",")[2][:3] out += csv_file[1].split(",")[3][:3] os.rename(filename, out + ".csv")
just drop in folder csv's renamed , run it
Comments
Post a Comment