1

I am learning Python and I have an exercise to work through:

Write a program which will read in lines from a file and select the first three fields (separated by blank space). The program should write a file called error which contains lines which look like Line 362 did not have 3 fields, to identify those which did not have 3 fields. The program should then write, into a file called output, the first three fields in the form of a string of width 10, followed by two floats of width 10 each with 3 places after the decimal point. In case this is not possible, the fields should be written as strings of width 10.

I have done half of it, but I can't get the list items into the error exceptions report. I don't get any error messages; I just get an empty file.

My exceptions data is displayed in Python but the write output is missing.

This is my code:

file = open("question3.txt",'r')
read = file.read()
file.close()
print(read)
#This is the list of sentences that form the list 'read'
#I need to analyse these list items.

array = read.splitlines()
print(array)
for item in array:
    if(item.count(" "))<3:    
        exception = str(item)
        f = open('error.txt','w')
        f.write(exception)
        print(exception)
        f.close()

How can I resolve this? I don't need a full answer to the question, just a suggestion as to how I can get the 'short list fields' I have identified into the text file.

Thank you!

Joel
  • 1,564
  • 7
  • 12
  • 20
Naz
  • 395
  • 1
  • 4
  • 12
  • 1
    Opening the error file in `w` mode removes the existing contents and creates an empty file. Try using `a` mode instead. – John Gordon Oct 01 '18 at 21:05
  • Hello guys. Thanks for your responses. Very quick. First time I have used stack overflow. I have solved my problem. Got past my immediate problem. I moved the f.open and f. close lines from the for loop. Sorry. I am a beginner. Second week of Python / Computer programmes – Naz Oct 01 '18 at 21:19
  • I am hoping that just by replying to my question you all know I am OK now. I don't see a general button to say problem solved. – Naz Oct 01 '18 at 21:22

3 Answers3

0

you can use

for idx, item in enumerate(array):

to keep track of the line number, and write a more informative error message.

jwzinserl
  • 427
  • 1
  • 3
  • 7
0

By default open(file, 'w') will delete any existing file with the name file and create a new file. So maybe you are iterating over these lines correctly, but with every loop you call open(file, 'w') erasing any lines you had. An easy fix for this would be to move it outside the for loop

f = open('error.txt','w')
for item in array:
   ... #etc

or you could use open(file, 'a') This will append to any existing file instead of rewriting it. See Python Doc StackOverflow Post for more details

JR ibkr
  • 869
  • 7
  • 24
0
file = open("question3.txt",'r') 
read = file.read() 
file.close() 
print(read)

array = read.splitlines()
print(array) 

# 1) Open in append mode using 'a' 
# 2) '+' tells python to create a file
# named error.txt if it does not exist.
# 3) Open file before initiating for loop

f = open('error.txt', 'a+') 

for item in array: 
    if item.count(" ") < 3:
        exception = str(item) + '\n' # Separate two exceptions with a newline
        f.write(exception)
        print(exception)

# Now close the file after for loop is done
f.close()

This should work. Please note that opening of file error.txt is done before entering for loop. Reason: In every iteration you only need appending a line to file and not opening file, appending line and closing file. So open your file once, do your stuff and then finally close it.

Paandittya
  • 885
  • 8
  • 17