Welcome, guest | Sign In | My Account | Store | Cart

A hack that saved me a lot of time. Well perhaps not so much, but it was funnier than to do it manually.

Python, 45 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
'''
generates a set of files by doing ASCII same size substitutions on an original file, controller by a csv file

 CSV FORMAT :
FILE NAME changes.csv 
first row: ignored  (colum names)
first column: filenames
Second row: original file name, strings to search
rest of rows: generated filename, strings to replace

USE
put this script, the csv and original file original in same folder, generated files go to the same folder

'''
import csv,sys
from codecs import ascii_encode
# lee csv
reader = list(csv.reader( open("changes.csv",  newline=''),delimiter=','))


# row 1 : original filename, strings to find
l0=reader[1]
items= len(l0)
#read original file as binary 
   
with open(l0[0],"rb") as f:
   s=bytes(f.read())
#generate output files
c=3   
for row in reader[2:]:
     l= row
     print(l)
     s1=s
     print (l)
     # do as many replaces as columns -1
     for i in range(1,items):
        #stop if replace is not same size as search 
        if len(l0[i])!=len(l[i]):
             print('Line ',c, l[i],' not same length as ', l0[i])
             sys.exit(0)
        s1=s1.replace(ascii_encode(l0[i])[0],ascii_encode(l[i])[0])
	#after replacements, write file	
     with open(l[0],"wb") as f1:
        f1.write(s1)
     c+=1    
       
       
Created by Antoni Gual on Wed, 16 Sep 2015 (MIT)
Python recipes (4591)
Antoni Gual's recipes (16)

Required Modules

  • (none specified)

Other Information and Tasks