{"id":23,"date":"2013-02-18T23:27:47","date_gmt":"2013-02-18T23:27:47","guid":{"rendered":"https:\/\/code4reference.com\/?p=23"},"modified":"2013-02-18T23:27:47","modified_gmt":"2013-02-18T23:27:47","slug":"code4referencea-simple-python-script-to-find-and-replace-text-string-in-a-file","status":"publish","type":"post","link":"https:\/\/code4reference.com\/?p=23","title":{"rendered":"Code4ReferenceA simple Python script to find and replace text string in a file"},"content":{"rendered":"<p>A little while ago I was trying to port my blogs from Blogger.com to code4reference.com. But when I tried to port the blog, I noticed that the code section of my blog contained<br \/>\nHTML tags for new line. When I used the the Syntaxhighlighter, the view was awful since the Syntax highter expects the code as written on text edittor without any<br \/>\n. \u00a0The Syntax highlighter treats \u00a0<br \/>\ntag as a string rather than as a new line. Now there was huge task to remove the unwanted<br \/>\ntag from my code sections. Well I started thinking how to make this task easier. No wonder, I thought of writing a small python script substituting<br \/>\nwith \u2018\\n\u2019. This script is improvised and doesn\u2019t cater to professional needs. However, one can learn to open file, parse the string, \u00a0substitute values, and create time temp file from this example code.<\/p>\n<pre>\n#!\/usr\/bin\/env python\nfrom tempfile import mkstemp\nfrom shutil import move\nfrom os import remove, close\nimport sys\n\ndef replace(filename, pattern, subst):\n    #Create temp file\n    fh, abs_path = mkstemp()\n    new_file = open(abs_path,'w')\n    old_file = open(filename)\n    for line in old_file:\n        new_file.write(line.replace(pattern, subst))\n    #close temp file\n    new_file.close()\n    close(fh)\n    old_file.close()\n    #Remove original file\n    remove(filename)\n    #Move new file\n    move(abs_path, filename)\n\ndef main():\n    if len(sys.argv) != 2:\n        print ('%(prog)s filename ')\n        exit()\n\n    replace(sys.argv[1],\"<br \/>\",'\\n')\n\nif __name__==\"__main__\":\n    \"\"\"If the this file  run as program then execute the main method\"\"\"\n    main()\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A little while ago I was trying to port my blogs from Blogger.com to code4reference.com. But when I tried to port the blog, I noticed that the code section of my blog contained HTML tags for new line. When I used the the Syntaxhighlighter, the view was awful since the Syntax highter expects the code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=23"}],"version-history":[{"count":0,"href":"https:\/\/code4reference.com\/index.php?rest_route=\/wp\/v2\/posts\/23\/revisions"}],"wp:attachment":[{"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code4reference.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}