In: Computer Science
in PYTHON
given a specific text file containing a list of numbers on each line (numbers on each line are different)
write results to a new file after the following tasks are performed:
Get rid of each line of numbers that is not 8 characters long
Get rid of lines that don't begin with (478, 932, 188, 642, 093)
Open Python IDLE and paste the code given below
Change the path given in project for infile and outFile
-------------------------------------------------------------------------------------------------------------------
def main():
#read file from path
infile =
open("C:\\Users\\PhoenixZone\\Desktop\\testfile.txt","r")
lines_list = infile.readlines()
infile.close()
#file to write the data
outFile =
open("C:\\Users\\PhoenixZone\\Desktop\\outfile.txt","w+")
for line in lines_list:
#Get rid of each line of numbers that is not 8 characters
long
if len(line)>8:
#Get rid of lines that don't begin with (478, 932, 188, 642,
093)
if line.startswith('478') or line.startswith('932') or
line.startswith('188') or line.startswith('642') or
line.startswith('093'):
#write line to output.txt
outFile.write(line)
main()
------------------------------------------------------------------------------------------------------
//testfile.txt
478 23 5 5 6
932 5 2 56
6 5 5
985 654 689
093 365 689 12 1
188 2 36 5 6 5 6
--------------------------------------------------------
//output.txt
478 23 5 5 6
932 5 2 56
093 365 689 12 1
188 2 36 5 6 5 6
-------------------------------------------------------