Python Forum
Basic Python 3 Win 10 Search Txt to File Newbie
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Python 3 Win 10 Search Txt to File Newbie
#1
Hello,
First post, but have spent hours online trying to get first usable program other than "Hello World" to work. All posted solutions found to have bugs on my setup - different Python versions(?).
Very simple for...next type algorithm with all "Basic" progs, VB, PowerBasic or batch file, but examples online don't work, so far. My first attempt to try out Python, due to all of the hoopla about its power etc.

Running Python 3.7.2 .py file from Win 10 "Desktop," I open a text file with several one line entries of credit card names. Some end with "VisaRen" indicating that I need to renew or update them.

I want an output text file of just those "Visa" flagged files. After a fairly exhaustive search, I found only one program that gives me 1 (one) result (one line of text) in an output file. I want all lines that match a search criterium.

Here is my attempt, admittedly copied from an online "expert's" example with double backslashes used instead of single backslashes as he recommended:

stringToMatch = 'VisaRen'
matchedLine = ''
 
#get line
# NOTE to self - " and \\ used for Python 3.7 instead of ' and \
with open("C:\\Users\\John\Desktop\\StuffIn.txt", "r") as file:
	for line in file:
		if stringToMatch in line:
			matchedLine = line
			break
 
#and write it to the file
with open("C:\\Users\\John\Desktop\\StuffOut.txt", "w") as file:
	file.write(line)
	#Above was (matchedLine)
	
# f = close() - ??
Any help appreciated. About to give up on Python!
Reply


Messages In This Thread
Basic Python 3 Win 10 Search Txt to File Newbie - by SnakeTyro - Apr-18-2019, 08:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  very newbie problem on text file zapad 2 265 Apr-12-2024, 06:50 PM
Last Post: zapad
  Basic binary search algorithm - using a while loop Drone4four 1 401 Jan-22-2024, 06:34 PM
Last Post: deanhystad
  Search Excel File with a list of values huzzug 4 1,288 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,425 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  search file by regex SamLiu 1 936 Feb-23-2023, 01:19 PM
Last Post: deanhystad
  If function is false search next file mattbatt84 2 1,173 Sep-04-2022, 01:56 PM
Last Post: deanhystad
  Python newbie laleebee 2 1,347 May-24-2022, 01:39 PM
Last Post: laleebee
  fuzzywuzzy search string in text file marfer 9 4,667 Aug-03-2021, 02:41 AM
Last Post: deanhystad
  Cloning a directory and using a .CSV file as a reference to search and replace bg25lam 2 2,162 May-31-2021, 07:00 AM
Last Post: bowlofred
  Basic python Natters10 3 3,118 Nov-29-2020, 07:04 AM
Last Post: Love2code

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020