如何使用python 2.7将文件中的文本/字符串替换为另一个文本/字符串

时间:2017-10-19 14:48:41

标签: python python-2.7

我写了下面的代码来替换字符串" YANTRAPRODPATH"与" YantraProd1"在文件中保存: -

$(document).ready(function() {
manageMemberTable = $("#manageMemberTable").DataTable({


    "ajax": "php_actionsms/retrieve.php",
    "order": [[0,'desc']]
});

但我收到以下错误: -

<div class="container">
    <div class="row">
        <div class="col-md-12">

            <center><h1 class="page-header">TMTRO Iloilo <small>Accident Report Records</small> </h1></center>

            <div class="removeMessages"></div>
            <br /> <br /> <br />

            <table class="table table-responsive " id="manageMemberTable">                  
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Recipient</th>
                        <th>Recipient Number</th>
                        <th>Place</th>
                        <th>Officer</th>
                        <th>Date&Time</th>                                              
                        <th>Sent to Icer</th>
                        <th>Option</th>
                    </tr>
                </thead>
            </table>
        </div>
    </div>
</div>

我在这里遗漏了什么吗?还有更简单的方法吗?

1 个答案:

答案 0 :(得分:1)

FileInput构造函数需要文件名或文件名列表,而不是file对象。

fileToModify = "C:/workspace/PROD/bat1/customer_overrides.properties"
textToSearch = "<YANTRAPRODPATH>"
textToReplace = "YantraProd1"

f = fileinput.FileInput(fileToModify, inplace=True, backup='.bak')

for line in f:
    print(line.replace(textToSearch, textToReplace))

f.close()