2

My code below checks the validity of postcodes with a regular expression. The postcodes are given in a form of an array. The for loop checks the validity of each postcode with the given regular expression. Now I want to write all the valid postcodes into a csv file. Below is my code:

import csv
import re

regex = r"(GIR\s0AA)|((([A-PR-UWYZ][0-9][0-9]?)|(([A-PR-UWYZ][A-HK-Y][0-9]((BR|FY|HA|HD|HG|HR|HS|HX|JE|LD|SM|SR|WC|WN|ZE)[0-9])[0-9])|([A-PR-UWYZ][A-HK-Y](AB|LL|SO)[0-9])|(WC[0-9][A-Z])|(([A-PR-UWYZ][0-9][A-HJKPSTUW])|([A-PR-UWYZ][A-HK-Y][0-9][ABEHMNPRVWXY]))))\s[0-9][ABD-HJLNP-UW-Z]{2})"

postcodes = ['$%±()()','XX XXX','A1 9A','LS44PL','Q1A 9AA','V1A 9AA','X1A 9BB','LI10 3QP','LJ10 3QP','LZ10 3QP','A9Q 9AA','AA9C 9AA','FY10 4PL','SO1 4QQ','EC1A 1BB','W1A 0AX','M1 1AE','B33 8TH','CR2 6XH','DN55 1PT','GIR 0AA','SO10 9AA','FY9 9AA','WC1A 9AA']


for x in postcodes:
    if(re.findall(regex,x)):
        with open('test2.csv','w',newline='') as fp:
            a = csv.writer(fp)
            a.writerows(x)

The problem with the code is it does not write all the valid postcodes into the csv file, instead it only write the last postcode (WC1A 9AA) in the following format:

W
C
1
A 

9
A
A

I don't know where I am making the mistake. Please help.

6
  • 1
    I think you want the with open( and the following line to come before your for loop Commented Jan 28, 2017 at 23:45
  • It works but the problem is it prints only one word in a row. For example the postcode WC1A 9AA is printed like how I wrote in the question above. Commented Jan 28, 2017 at 23:48
  • writerows() expects the argument to be a list of rows, you're calling it with a single string. So it splits the string up into a list of characters, and each character is a row. Commented Jan 28, 2017 at 23:49
  • You should be calling writerow() to write one row at a time. Commented Jan 28, 2017 at 23:49
  • You could also use a list comprehension to get a list of all the postcodes that match the regexp, and then write that with writerows(). Commented Jan 28, 2017 at 23:50

3 Answers 3

2

There a few issues but the biggest one is the 'w' -- you're wiping out the file each time you write to it! :) Change that to an 'a' for append.

Secondly I'm not sure what you're attempting to do, but if you're trying to write them all on seperate rows

codes = []
for x in postcodes:
    if(re.findall(regex,x)):
       codes.append([x])

with open('test2.csv','w',newline='') as fp:
    a = csv.writer(fp)
    a.writerows(codes)
Sign up to request clarification or add additional context in comments.

5 Comments

The code works fine but the problem is it print one character in a column. Like the postcode 'LI10 3QP' is written as L,I,1,0, ,3,Q,P. I want to write the complete postcode in one column like LI10 3QP.
are you trying to write each on a seperate row or all in one row?
each postcode in a seperate row but a complete postcode in one column.
something like this: 'WC1 100' in 1st row 1st column, 'LC12 P12' in 2nd row 1st column, 'QW12 O23' in 3rd row 1st column....
1

With the open file command set with flag "w" in the Loop, it delete and creates a new file in every iteration, hence you only getting the last postcode. This should get you the correct result:

with open('test2.csv','w',newline='') as fp:
    for x in postcodes:
        if(re.findall(regex,x)):
            a = csv.writer(fp)
            a.writerows(x)

Yeah, I forgot. "fp.close()" unnessesary with the "with open" statement.

3 Comments

with open doesn't close the file by itself ?
it does, no need for the fp.close()
The code works fine but the problem is it print one character in a column. Like the postcode 'LI10 3QP' is written as L,I,1,0, ,3,Q,P. I want to write the complete postcode in one column like LI10 3QP.
0

Change the 'w' to 'a' mode in the open function to append rather than overwrite the file on each loop, or simply move the for loop inside the with context manager, as suggested by @jcfollower.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.