Prev: Need Translation library
Next: Replace and inserting strings within .txt files with the useof regex
From: Νίκος on 7 Aug 2010 23:35 On 8 Áýã, 05:56, John S <jstrick...(a)gmail.com> wrote: >"How can I use RE string replacement to find PHP tags and convert them >to Django template tags?" No, not at all John, at least not yet! I have only 1 week that i'm learnign python(changing from php & perl) so i'm very fresh at this beautifull and straighforwrd language. When i have a good understnading of Python then i will proceed to Django templates. Until then my Python templates would be only 'simple html files' that the only thign they contain apart form the html data would be the special string formatting identifies '%s' :-)
From: Νίκος on 8 Aug 2010 04:30 On 8 ÎÏγ, 11:09, Steven D'Aprano <st...(a)REMOVE-THIS- cybersource.com.au> wrote: > On Sat, 07 Aug 2010 17:20:24 -0700, ÎÎ¯ÎºÎ¿Ï wrote: > > I don't know how to handle such a big data replacing problem and cannot > > play with fire because those 500 pages are my cleints pages and data of > > those filesjust cannot be messes up. > > Take a backup copy of the files, and only edit the copies. Don't replace > the originals until you know they're correct. > > -- > Steven Yes of course, but the code that John S provided need soem modification in order to be able to change various instances of php tags and not only one set.
From: Νίκος on 8 Aug 2010 05:21 Script so far: #!/usr/bin/python import cgitb; cgitb.enable() import cgi, re, os print ( "Content-type: text/html; charset=UTF-8 \n" ) id = 0 # unique page_id for currdir, files, dirs in os.walk('data'): for f in files: if f.endswith('php'): # get abs path to filename src_f = join(currdir,f) # open php src file f = open(src_f, 'r') src_data = f.read() # read contents of PHP file f.close() print 'reading from %s' % src_f # replace tags src_data = src_data.replace('<%', '') src_data = src_data.replace('%>', '') print 'replacing php tags' # add ID src_data = ( '<!-- %d -->' % id ) + src_data id += 1 print 'adding unique page_id' # create new file with .html extension src_file = src_file.replace('.php', '.html') # open newly created html file for insertid data dest_f = open(src_f, 'w') dest_f.write(src_data) # write contents dest_f.close() print 'writing to %s' % dest_f Please help me adjust it, if need extra modification for more php tags replacing.
From: Peter Otten on 9 Aug 2010 03:38 Νίκος wrote: > Now the code looks as follows: > for currdir, files, dirs in os.walk('test'): > > for f in files: > > if f.endswith('php'): > > # get abs path to filename > src_f = join(currdir, f) > I just tried to test it. I created a folder names 'test' in me 'd:\' > drive. > Then i have put to .php files inside form the original to test if it > would work ok for those too files before acting in the whole copy and > after in the original project. > > so i opened a 'cli' form my Win7 and tried > > D:\>convert.py > > D:\> > > Itsjust printed an empty line and nothign else. Why didn't even try to > open the folder and fiels within? > Syntactically it doesnt ghive me an error! > Somehting with os.walk() methos perhaps? If there is a folder D:\test and it does contain some PHP files (double- check!) the extension could be upper-case. Try if f.lower().endswith("php"): ... or php_files = fnmatch.filter(files, "*.php") for f in php_files: ... Peter
From: Νίκος on 9 Aug 2010 04:22 On 9 ÎÏγ, 10:38, Peter Otten <__pete...(a)web.de> wrote: > ÎÎ¯ÎºÎ¿Ï wrote: > > Now the code looks as follows: > > for currdir, files, dirs in os.walk('test'): > > >     for f in files: > > >         if f.endswith('php'): > > >             # get abs path to filename > >             src_f = join(currdir, f) > > I just tried to test it. I created a folder names 'test' in me 'd:\' > > drive. > > Then i have put to .php files inside form the original to test if it > > would work ok for those too files before acting in the whole copy and > > after in the original project. > > > so i opened a 'cli' form my Win7 and tried > > > D:\>convert.py > > > D:\> > > > Itsjust printed an empty line and nothign else. Why didn't even try to > > open the folder and fiels within? > > Syntactically it doesnt ghive me an error! > > Somehting with os.walk() methos perhaps? > > If there is a folder D:\test and it does contain some PHP files (double- > check!) the extension could be upper-case. Try > > if f.lower().endswith("php"): ... > > or > > php_files = fnmatch.filter(files, "*.php") > for f in php_files: ... > > Peter The extension is in in lower case. folder is there, php files is there, i dont know why it doesnt't want to go into the d:\test to find them. Thast one problem. The other one is: i made the code simpler by specifying the filename my self. ========================= # get abs path to filename src_f = 'd:\\test\\index.php' # open php src file print ( 'reading from %s' % src_f ) f = open(src_f, 'r') src_data = f.read() # read contents of PHP file f.close() ========================= but although ti nwo finds the fiel i egt this error in 'cli': D:\>aconvert.py reading from d:\test\index.php Traceback (most recent call last): File "D:\aconvert.py", line 16, in <module> src_data = f.read() # read contents of PHP file File "C:\Python32\lib\encodings\cp1253.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x9f in position 321: char acter maps to <undefined> Somethign with the damn encodings again!!
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Need Translation library Next: Replace and inserting strings within .txt files with the useof regex |