可能重複不存在的話:
Add a line to a file if it not exist using python行添加到一個文本文件,如果使用python
我有一個文本文件,如下所示:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0">
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
我想要在 之間添加一行<Import Project="$(ProjectName).targets" />
</ImportGroup>
和</Project>
如下
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0">
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
<Import Project="$(ProjectName).targets" />
</Project>
我用的FileInput模塊插入線,但我不希望添加 <Import Project="$(ProjectName).targets" />
如果它已經在文件 存在怎樣才能做到這一點?
無不是一個重複 這是一個XML文件,除H我iam問一個文本文件和一個文本文件不能使用任何xml API讀取。所以我重新發布它。我能夠使用FileInput模塊添加行,只是要求你只關於避免重複 – user1565954
xml格式的文件應該可以使用xml解析器或html解析器(如xmlTree或Beautifulsoup)來處理... ... –
http://stackoverflow.com/questions/12275255/add-a-line-to-a-file-if-it-not-exist-using -python ...沒有人... –