简体   繁体   English

在文本文件中查找特定单词并打印输出

[英]Find specific words in a text file and print the ouput

I need help on python code我需要有关 python 代码的帮助

I have a text file with around 10 lines of paragraph.我有一个大约 10 行段落的文本文件。 Below is an example下面是一个例子

"406%2C318482214%2C318484497%2C318486317%2C318484676%2C318483611%2C318484802%2C318487059%2C318489974%2C318482672%2C318475417&tag_for_child_directed_treatment=0&_c_csdk_npa_o=false&_c_req_tfua=false&_c_req_npa=true& npa=1 & tfua =0&guci=0.0.0.0.0.0.0.8&rbv=1&u_w=839&u_h=424&msid=com.ea.gp.test&_package_name=com.ea.gp.test&an=30178.android.com.ea.gp.test&net=wi&u_audio=3&u_so=l&preqs_in_session=0&support_transparent_background=true" "406%2C318482214%2C318484497%2C318486317%2C318484676%2C318483611%2C318484802%2C318487059%2C318489974%2C318482672%2C318475417&tag_for_child_directed_treatment=0&_c_csdk_npa_o=false&_c_req_tfua=false&_c_req_npa=true& npa=1 & tfua =0&guci=0.0.0.0.0.0.0.8&rbv=1&u_w=839&u_h =424&msid=com.ea.gp.test&_package_name=com.ea.gp.test&an=30178.android.com.ea.gp.test&net=wi&u_audio=3&u_so=l&preq=3&u_so=l&preq=true

How can i write a python code to find the value assigned to the specific word "npa" & "tfua".我如何编写 python 代码来查找分配给特定单词“npa”和“tfua”的值。 Lets assume the highlighted once in the above text.让我们假设在上面的文本中突出显示一次。 It might happen that those words repeat multiple times also.这些词也可能重复多次。

Suppose your string is myStr假设你的字符串是 myStr

myStr = "406%2C318482214%2C318484497%2C318486317%2C318484676%2C318483611%2C318484802%2C318487059%2C318489974%2C318482672%2C318475417&tag_for_child_directed_treatment=0&_c_csdk_npa_o=false&_c_req_tfua=false&_c_req_npa=true&npa=1&tfua=0&guci=0.0.0.0.0.0.0.8&rbv=1&u_w=839&u_h=424&msid=com.ea.gp.test&_package_name=com.ea.gp.test&an=30178.android.com.ea.gp.test&net=wi&u_audio=3&u_so=l&preqs_in_session=0&support_transparent_background=true"
myLst = [lst.split("=") for lst in myStr.split("&")]
myDict = {lst[0] : lst[1] for lst in myLst}

Now myDict["npa"] gives the desired value.现在myDict["npa"]给出所需的值。 Likewise for myDict["tfua"]同样对于myDict["tfua"]

If you are sure you only want those two you could restrict the dict to only those values:如果您确定只需要这两个,则可以将 dict 限制为仅这些值:

myLst = [lst.split("=") for lst in myStr.split("&")]
myDict = {lst[0] : lst[1] for lst in myLst if lst[0] in ["npa", "tfua"]}

Using the re module使用re模块

import re
x = "406%2C318482214%2C318484497%2C318486317%2C318484676%2C318483611%2C318484802%2C318487059%2C318489974%2C318482672%2C318475417&tag_for_child_directed_treatment=0&_c_csdk_npa_o=false&_c_req_tfua=false&_c_req_npa=true&npa=1&tfua=0&guci=0.0.0.0.0.0.0.8&rbv=1&u_w=839&u_h=424&msid=com.ea.gp.test&_package_name=com.ea.gp.test&an=30178.android.com.ea.gp.test&net=wi&u_audio=3&u_so=l&preqs_in_session=0&support_transparent_background=true"

npa=re.findall('(?<=npa=)(.*?)(?=&)', x)
tfua=re.findall('(?<=tfua=)(.*?)(?=&)', x)


print(npa)
>>> ['True','1']

print(tfua)
>>> ['False','0']

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

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