简体   繁体   English

如何从文本文件中提取特定文本

[英]How to extract specific text from text file

Hello Stackoverflow community,你好 Stackoverflow 社区,

I have problem regarding my code that I am trying to retrieve some specific text from a text file, Which I am able to to do.我的代码有问题,我试图从文本文件中检索一些特定的文本,我可以做到。 But I am using the seek method to retrieve the data.但我正在使用 seek 方法来检索数据。 But for that I am giving the starting position of the text and the end position of the text.但为此,我给出了文本的开头 position 和文本的结尾 position。 Which gives me the exact output what I want.这给了我想要的确切 output 。 But some times the length of the retrieving text might be longer then my code doesn't retrieve the whole text.但有时检索文本的长度可能会更长,然后我的代码无法检索整个文本。 So how can I do that那我该怎么做

I am using python2.7 and trying to retrieve the specific data from a text file我正在使用 python2.7 并尝试从文本文件中检索特定数据

file = open("C:\Users\This_PC\p4.txt", "r")
file.seek(645)
string = file.read(13 - 0)
print string

I am getting the out put as expected like test_label123 .我得到了预期的输出,如test_label123 But when in the text file if the length of the text is longer like test_label12345 then I am getting the output as test_label123 which is wrong.但是当在文本文件中,如果文本的长度像test_label12345那样长,那么我将 output 作为test_label123得到,这是错误的。

p4.txt content is mentioned below p4.txt 内容在下面提到

# A Perforce Label Specification.
#
#  Label:       The label name.
#  Update:      The date this specification was last modified.
#  Access:      The date of the last 'labelsync' on this label.
#  Owner:       The user who created this label.
#  Description: A short description of the label (optional).
#  Options:     Label update options: [un]locked, [no]autoreload.
#  Revision:    Optional revision specification to make an automatic label.
#  ServerID:    If set, restricts access to the named server.
#  View:        Lines to select depot files for the label.
#
# Use 'p4 help label' to see more about label views.

Label:  test_label123

Owner:  This_PC

Description:
    Created by Auto12

Options:    unlocked noautoreload

You may use regular expressions , so something similar to this您可以使用正则表达式,因此类似于此

import re

file = open("test.txt", "r")
string = file.read()
m = re.search('(?<=Label:) {0,2}(\w+)', string)

print m.group(1)

You may need to tweak your regexp to properly read the second apperance and not the first one ( Label: The label name. )您可能需要调整您的正则表达式以正确读取第二个外观而不是第一个( Label: The label name.

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

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