简体   繁体   English

在没有用户输入的情况下替换/删除打开的文本文件中的子字符串(Python 3)

[英]Replace/delete substring in opened text file without user input (Python 3)

How to use Python methods for replacing substrings (or deleting part of string) in an manually opened .txt file. 如何使用Python方法替换手动打开的.txt文件中的子字符串(或删除部分字符串)。 Text file is already opened by the user and python script should replace substring in that file with space (or delete it, whatever is easier). 用户已经打开了文本文件,并且python脚本应使用空格替换该文件中的子字符串(或删除它,更方便)。

The string text file looks like this (it was part of the website and it can have multiple lines, but I put only first line for simplicity): 字符串文本文件如下所示(它是网站的一部分,可以包含多行,但为简单起见,我仅放置第一行):

<tr><td>Subtitle</td><td><div class="Current temperature"><!-- here -- <!-- here2 --><div class="London"></div></div><div class="here3"></div><div class="Current temperature"><!-- here4 <!-- /react-text --><div class="Paris"></div></div></td></tr>

The final result should be in two lines: 最终结果应为两行:

London
Paris

I've already searched a lot of topics here, a lot of them assumes user input of the string that should be replaced and another input for the new string. 我已经在这里搜索了很多主题,其中许多假设用户输入应该替换的字符串,而另一个输入则是新字符串。

This little script is not of that type, it should already contain the substring to be replaced (without user input), so this parts: 这个小脚本不是那种类型,它应该已经包含要替换的子字符串(没有用户输入),因此这部分内容:

1. <tr><td>Subtitle</td><td><div class="Current temperature"><!-- here -- <!-- here2 --><div class=" 1. <tr><td>Subtitle</td><td><div class="Current temperature"><!-- here -- <!-- here2 --><div class="
2. "></div></div><div class="here3"></div><div class="Current temperature"><!-- here4 <!-- /react-text --><div class=" 2. "></div></div><div class="here3"></div><div class="Current temperature"><!-- here4 <!-- /react-text --><div class="
3. "></div></div></td></tr> 3. "></div></div></td></tr>

and it should do it automatically when run. 并且它应该在运行时自动执行。 I also have problems with quotation marks and html tags so I get syntax errors often. 我在引号和html标记方面也遇到了问题,因此经常会出现语法错误。

On other topics about this theme I found this and tried this code: 在与此主题有关的其他主题上,我找到了这个并尝试了以下代码:

file = open("C:/Users/blue11440/Desktop/example-text-file.txt", "r")
str = file.readlines()

x = str.replace("<tr><td>Subtitle</td><td><div class="Current temperature"> 
<!-- here -- <!-- here2 --><div class="", "")

Among other errors I get this: 除其他错误外,我得到这个:

AttributeError: 'list' object has no attribute 'replace'
AttributeError: '_io.TextIOWrapper' object has no attribute 'replace'

I tried replace, delete, remove methods with little succes, is there any better way? 我尝试了很少的replace, delete, remove方法,还有什么更好的方法吗?

For your last problem: file.readlines() returns a list which contains an element for each line. 对于您的最后一个问题:file.readlines()返回一个包含每行元素的列表。

.replace() is a string (!) method. .replace()是字符串(!)方法。 So try to iterate over the list: 因此,尝试遍历该列表:

for line in file.readlines():
    line.replace(....)

Edit: I think you have a problem with the " in the string: str.replace("Subtitle 编辑:我认为您在字符串中有一个“问题:str.replace(” Subtitle

Use single quotes to avoid this: str.replace('Subtitle 使用单引号可以避免这种情况:str.replace('Subtitle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM