试图从包含奇怪字符的文件中读取某些文本。 (Python)
Trying to read certain text from a file that has strange characters. (Python)
您好,我正在尝试从文本文档中的关键字中获取数据作为项目,我可以使用此代码来完成此操作。我是 python 的新手,我不确定从哪里开始解决这个问题。
data_file = open("test.txt", "r")
Keyword = raw_input("Please enter the keyword: ")
go = False
start = Keyword
end = "[+][+]"
with open("test.txt") as infile:
for line in infile:
line = line.strip()
if start in line: go = True
elif end in line:
go = False
continue
if go:
print(line)
此代码非常适合像
这样的示例文本文档
Something Something Something Something
Something Something Something Something
Something Keyword:
Data
Data
Data
Data
End
Something
但是,我 运行 在尝试读取包含奇怪字符的文件时遇到了问题。例如:
2015/08/14 15:48:30 OUT:
2015/08/14 15:48:30 OUT:
PQ=
(3< ’’aÈ©ÿY˜ü â [+][+]52
2015/08/14 15:48:31:IN[+]53[+][+]101[+]-1[+] **Keyword** ,SHOWALL
**data**
**data**
**data**
**data**
**data**
**data**
**data**
end
因为我们的目标是阅读这个文本文档并打印出 Keyword 和 End 之间的单词。如果其中包含这些字符,它将不会执行。对于这个项目,我不能删除这些字符,它只需要能够通读文档并找到关键字并打印出它们之间的内容。
关于如何从包含这些奇怪字符的文本文档中读取并正确处理而不是崩溃的任何想法。
该文件包含二进制内容,因此应以二进制模式打开它
你可以这样做
data_file = open("test.txt", "rb")
首先您需要以二进制模式打开文件。然后,您可以使用正则表达式提取输入的关键字和 "end" 之间的所有文本。然后可以使用另一个正则表达式提取整个单词:
import re
with open("input.txt", "rb") as f_input:
start_token = raw_input("Please enter the start keyword: ")
end_token = raw_input("Please enter the end keyword: ")
reText = re.search("%s(.*?)%s" % (re.escape(start_token), re.escape(end_token)), f_input.read(), re.S)
if reText:
for word in re.findall(r"\b\w+\b", reText.group(1)):
print word
else:
print "not found"
对于您的示例文本,这将显示:
SHOWALL
data
data
data
data
data
data
data
或者如果您只想要两点之间的所有文本,print reText.group(1)
而不是 for
循环。
更新: 添加了对可变结束标记的支持。
您好,我正在尝试从文本文档中的关键字中获取数据作为项目,我可以使用此代码来完成此操作。我是 python 的新手,我不确定从哪里开始解决这个问题。
data_file = open("test.txt", "r")
Keyword = raw_input("Please enter the keyword: ")
go = False
start = Keyword
end = "[+][+]"
with open("test.txt") as infile:
for line in infile:
line = line.strip()
if start in line: go = True
elif end in line:
go = False
continue
if go:
print(line)
此代码非常适合像
这样的示例文本文档Something Something Something Something
Something Something Something Something
Something Keyword:
Data
Data
Data
Data
End
Something
但是,我 运行 在尝试读取包含奇怪字符的文件时遇到了问题。例如:
2015/08/14 15:48:30 OUT:
2015/08/14 15:48:30 OUT:
PQ=
(3< ’’aÈ©ÿY˜ü â [+][+]52
2015/08/14 15:48:31:IN[+]53[+][+]101[+]-1[+] **Keyword** ,SHOWALL
**data**
**data**
**data**
**data**
**data**
**data**
**data**
end
因为我们的目标是阅读这个文本文档并打印出 Keyword 和 End 之间的单词。如果其中包含这些字符,它将不会执行。对于这个项目,我不能删除这些字符,它只需要能够通读文档并找到关键字并打印出它们之间的内容。
关于如何从包含这些奇怪字符的文本文档中读取并正确处理而不是崩溃的任何想法。
该文件包含二进制内容,因此应以二进制模式打开它
你可以这样做
data_file = open("test.txt", "rb")
首先您需要以二进制模式打开文件。然后,您可以使用正则表达式提取输入的关键字和 "end" 之间的所有文本。然后可以使用另一个正则表达式提取整个单词:
import re
with open("input.txt", "rb") as f_input:
start_token = raw_input("Please enter the start keyword: ")
end_token = raw_input("Please enter the end keyword: ")
reText = re.search("%s(.*?)%s" % (re.escape(start_token), re.escape(end_token)), f_input.read(), re.S)
if reText:
for word in re.findall(r"\b\w+\b", reText.group(1)):
print word
else:
print "not found"
对于您的示例文本,这将显示:
SHOWALL
data
data
data
data
data
data
data
或者如果您只想要两点之间的所有文本,print reText.group(1)
而不是 for
循环。
更新: 添加了对可变结束标记的支持。