{"id":416,"date":"2023-02-13T10:52:24","date_gmt":"2023-02-13T10:52:24","guid":{"rendered":"https:\/\/tinyytopic.com\/?p=416"},"modified":"2023-02-13T10:51:12","modified_gmt":"2023-02-13T10:51:12","slug":"find-a-word-in-a-text-file-using-python-ready-to-use-function","status":"publish","type":"post","link":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/","title":{"rendered":"Find a Word in a Text file using Python ready-to-use function"},"content":{"rendered":"\n<div class=\"wp-block-uagb-advanced-heading uagb-block-dba96ecc\"><h5 class=\"uagb-heading-text\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><br>How to find a Word in a Text file using Python ready-to-use function?<\/mark><\/h5><\/div>\n\n\n\n<p style=\"font-size:15px\">Ready to use Python function to find a word in a text file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"atomic\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">def find_string_in_text_file(txtFile, findWord, case_sensitive=False):\n    # Check if 'findWord' string exists in text file\n    with open(txtFile, 'r') as file:\n        lines = file.readlines()\n        for row in lines:\n            # remove new line character if exists\n            row = row.replace(\"\\n\", \"\")\n            \n            # change to loawer case if case senstive is false\n            if case_sensitive == False:\n                row = row.lower()\n                findWord = findWord.lower()\n            \n            # Check string is matching and length of the strings are matching\n            if row.find(findWord) >= 0:\n                return True\n        return False # if no match, return False<\/pre>\n\n\n\n<p style=\"font-size:15px\">Write your main code as a sample below, and save a text file named &#8216;test python.txt&#8217; in the same directory by the inclusion of the word &#8216;request&#8217; in the text file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>print(find_word_in_text_file(\"test python.txt\", \"request\"))<\/code><\/pre>\n\n\n\n<p style=\"font-size:15px\">The output of the code is,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>True<\/code><\/pre>\n\n\n\n<div class=\"wp-block-uagb-advanced-heading uagb-block-4b8f5593\"><h5 class=\"uagb-heading-text\"><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">How does the function work?<\/mark><\/h5><\/div>\n\n\n\n<p style=\"font-size:15px\">This code defines a function named &#8220;find_string_in_text_file&#8221;. The function takes three arguments:<\/p>\n\n\n\n<ol class=\"wp-block-list\" style=\"font-size:15px\">\n<li>The <strong><code>txtFile<\/code><\/strong>: The name of the text file that needs to be searched.<\/li>\n\n\n\n<li>The <strong><code>findWord<\/code><\/strong>: The string that needs to be searched in the text file.<\/li>\n\n\n\n<li>The <strong><code>case_sensitive<\/code><\/strong>: A boolean value that determines if the search should be case-sensitive or not. If it&#8217;s set to False, the code will search for the string in a case-insensitive manner.<\/li>\n<\/ol>\n\n\n\n<p style=\"font-size:15px\">The function uses the <strong><code>with open<\/code><\/strong> statement to open the text file specified in the <strong><code>txtFile<\/code><\/strong> argument. It then reads all the lines of the file using the <code><strong>readlines <\/strong><\/code>method and stores the lines in a list called <strong><code>lines<\/code><\/strong>.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>Next, the code uses a for loop to iterate through each line in the <strong><code>lines <\/code><\/strong>list. For each line, it removes the newline character (if it exists) using the <strong><code>replace <\/code><\/strong>method and converts the line to lowercase if the <strong><code>case_sensitive <\/code><\/strong>argument is set to False. This ensures that the search for the string is case-insensitive.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>The <code><strong>find<\/strong> <\/code>method is then used to check if the <strong><code>findWord <\/code><\/strong>string is present in the current line. If the <strong><code>find <\/code><\/strong>method returns a value greater than or equal to 0, it means that the string was found, and the function returns <strong><code>True<\/code><\/strong>.<\/p>\n\n\n\n<p style=\"font-size:15px\"><br>If the loop completes without finding a match, the function returns <strong><code>False<\/code><\/strong>, indicating that the string was not found in the text file.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ready to use Python function to find a word in a text file: Write your main code as a sample below, and save a text file named &#8216;test python.txt&#8217; in the same directory by the inclusion of the word &#8216;request&#8217; in the text file. The output of the code is, This code defines a function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12,17],"tags":[14,13,16,15,21],"class_list":["post-416","post","type-post","status-publish","format-standard","hentry","category-python","category-useful-function","tag-programming-language","tag-python","tag-python-code","tag-python-sample-code","tag-text-file"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Find a Word in a Text file using Python ready-to-use function - tinyytopic.com<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Find a Word in a Text file using Python ready-to-use function - tinyytopic.com\" \/>\n<meta property=\"og:description\" content=\"Ready to use Python function to find a word in a text file: Write your main code as a sample below, and save a text file named &#8216;test python.txt&#8217; in the same directory by the inclusion of the word &#8216;request&#8217; in the text file. The output of the code is, This code defines a function [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/\" \/>\n<meta property=\"og:site_name\" content=\"tinyytopic.com\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-13T10:52:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-02-13T10:51:12+00:00\" \/>\n<meta name=\"author\" content=\"tinyytopic.com\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"tinyytopic.com\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/\",\"url\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/\",\"name\":\"Find a Word in a Text file using Python ready-to-use function - tinyytopic.com\",\"isPartOf\":{\"@id\":\"https:\/\/tinyytopic.com\/#website\"},\"datePublished\":\"2023-02-13T10:52:24+00:00\",\"dateModified\":\"2023-02-13T10:51:12+00:00\",\"author\":{\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\"},\"breadcrumb\":{\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/tinyytopic.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Find a Word in a Text file using Python ready-to-use function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tinyytopic.com\/#website\",\"url\":\"https:\/\/tinyytopic.com\/\",\"name\":\"tinyytopic.com\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tinyytopic.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb\",\"name\":\"tinyytopic.com\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g\",\"caption\":\"tinyytopic.com\"},\"sameAs\":[\"http:\/\/tinyytopic.com\"],\"url\":\"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Find a Word in a Text file using Python ready-to-use function - tinyytopic.com","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/","og_locale":"en_US","og_type":"article","og_title":"Find a Word in a Text file using Python ready-to-use function - tinyytopic.com","og_description":"Ready to use Python function to find a word in a text file: Write your main code as a sample below, and save a text file named &#8216;test python.txt&#8217; in the same directory by the inclusion of the word &#8216;request&#8217; in the text file. The output of the code is, This code defines a function [&hellip;]","og_url":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/","og_site_name":"tinyytopic.com","article_published_time":"2023-02-13T10:52:24+00:00","article_modified_time":"2023-02-13T10:51:12+00:00","author":"tinyytopic.com","twitter_card":"summary_large_image","twitter_misc":{"Written by":"tinyytopic.com","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/","url":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/","name":"Find a Word in a Text file using Python ready-to-use function - tinyytopic.com","isPartOf":{"@id":"https:\/\/tinyytopic.com\/#website"},"datePublished":"2023-02-13T10:52:24+00:00","dateModified":"2023-02-13T10:51:12+00:00","author":{"@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb"},"breadcrumb":{"@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/tinyytopic.com\/index.php\/2023\/02\/13\/find-a-word-in-a-text-file-using-python-ready-to-use-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/tinyytopic.com\/"},{"@type":"ListItem","position":2,"name":"Find a Word in a Text file using Python ready-to-use function"}]},{"@type":"WebSite","@id":"https:\/\/tinyytopic.com\/#website","url":"https:\/\/tinyytopic.com\/","name":"tinyytopic.com","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tinyytopic.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/56c840cea8539fb221a03c5fa2ef32eb","name":"tinyytopic.com","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tinyytopic.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5f153681c8ca1e6d7287d858de51f968bb687221c89cf96d763ead4393881029?s=96&d=mm&r=g","caption":"tinyytopic.com"},"sameAs":["http:\/\/tinyytopic.com"],"url":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"}]}},"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"tinyytopic.com","author_link":"https:\/\/tinyytopic.com\/index.php\/author\/mmkmuthukumar21gmail-com\/"},"uagb_comment_info":78,"uagb_excerpt":"Ready to use Python function to find a word in a text file: Write your main code as a sample below, and save a text file named &#8216;test python.txt&#8217; in the same directory by the inclusion of the word &#8216;request&#8217; in the text file. The output of the code is, This code defines a function&hellip;","_links":{"self":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/comments?post=416"}],"version-history":[{"count":12,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/416\/revisions"}],"predecessor-version":[{"id":451,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/posts\/416\/revisions\/451"}],"wp:attachment":[{"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/media?parent=416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/categories?post=416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tinyytopic.com\/index.php\/wp-json\/wp\/v2\/tags?post=416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}