How to read textfile with some words in python

时间:2017-04-24 17:29:05

标签: python string file

i'm new in python please kindly help my stuck, i have code like bellow:

fobj = open("data.txt") 
text = fobj.read().strip().split()

Conditions

while True:
    s = input("Enter a string: ") #Takes input of a string from user
    if s == "": #if no value is entered for the string
        continue
    if s in text: #string in present in the text file
        print(s)
        break
    else: #string is absent in the text file
        print("No such string found,try again")
        continue fobj.close()

If i search in complete word in file .txt the result is ok, but if i search some word, my result "No such string found,try again"

example :

Enter a string: oke
oke

> ================================ RESTART ================================
>
Enter a string: ok
No such string found,try again

i want if i write "ok" the result must be "oke bla bla" with one line kindly please help my stuck, thanks in advance

1 个答案:

答案 0 :(得分:1)

您的问题来自于您已从文件中读取文本并将其拆分为空格。这样做可以通过字符串中的空格将文本标记为字符串列表。

e.g。 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="script.js"></script> <link rel="stylesheet" type="text/css" href="styles.css"> <script type="text/javascript"></script> <title>Travail pratique 3</title> <h1>Travail pratique #3</h1> <h2>Identification</h2> </p> <p>Publi&eacute; sur mon tp3 &aacute; la DESI </p> <style> #toggleThis{ margin-bottom: 5px; background: #e5eecc; border: solid; border:#c3c3c3; border: 1px; } #partie4{ background-color: #e5eecc; margin-top:50px; display:none; } </style> </head> <body> <h2>Partie 1: Affichage et animation des images</h2> <div id="radiobuttons"> <label><input name="vf" type="radio" onclick="changeImage('http://i.imgur.com/0zfjKQi.jpg')" value="0" checked/>Salade</label> <br> <label><input name="vf" type="radio" onclick="changeImage('http://i.imgur.com/OixGjjE.jpg')" value="1"/>Spaghetti</label><br> <label><input name="vf" type="radio" onclick="changeImage('http://i.imgur.com/gASTpLX.jpg')" value="2"/>Creme glacee</label> <br><br><br><br><br><br><br> </div> <img id="food"> <h2>Partie 2: Affichage dans la page web contr&ocirc;l&eacute; par JavaScript</h2> <form> Nom: <input type="text" name="fullname"><br><br> Niveau (1 &aacute; 6): <input type="text" name="niveau"> <input type="button" value="Afficher partie 2"> </form> <div class="Affichage"> </div> <h2>Partie 3: Formulaire de conversion</h2> Valeur:<input type="text" id="value" name="valeur" value="0"> <p id="result">Resultat ici</p> <button type="button" id="inchToCm" onclick="pvc('inch')">Pouces vers cm</button> <button type="button" id="cmtoInch" onclick="pvc('cm')">CM vers pouces</button> <button type="button" id="celciusToFarenheit" onclick="pvc('celc')">Celcius vers Farenheit</button> <button type="button" id="farenheitToCelcius" onclick="pvc('fahr')">Farenheit vers Celcius</button> <div id="toggleThis"> <h2>Partie 4: JQuery (cliquez ici)</h2> <div id="partie4">Voici le contenu de la partie 4, cliquez de nouveau sur le titre</div> <script> function changeImage(imgUrl){ document.querySelector("#food").src=imgUrl; } </script> <script> function do_stuff() { var x = document.getElementsByName("fullname")[0].value; var y = document.getElementsByName("niveau")[0].value; if (parseInt(y)>=1 && parseInt(y)<=6){ document.body.appendChild(document.createTextNode("Bonjour "+x)); document.body.appendChild(document.createTextNode("Niveau='"+y+"'")); } else { var p = document.createElement('P'); p.style.color = "red"; p.appendChild(document.createTextNode("Erreur le niveau doit etre en 1 et 6")); document.body.appendChild(p); } } document.addEventListener( "DOMContentLoaded", function() { for(let input of document.getElementsByTagName("input")) { if (input.type == "button") { input.addEventListener( "click", do_stuff ); } } }, false ); </script> <script> function pvc(val) { var input = document.getElementById("value").value; var result = document.getElementById("result"); if (val == 'inch') { result.innerHTML = input / 0.39370; } else if (val == 'cm') { result.innerHTML = input * 0.39370; } else if (val == 'celc') { result.innerHTML = 9 / 5 + 32; } else if (val == 'fahr') { result.innerHTML = (input - 32) * 5 / 9; } } </script> </body> </html> 变为"This is a test".split()

因此,您通过["this", "is", "a", "test"]读取的任何输入(完整字符串)都必须以类似的方式进行拆分和比较。

最简单的方法是在空白处不要input,并且您的比较应该按预期工作。