简体   繁体   中英

Python: Print statement shows all lines but writes only last line to file

This is my proof of concept script with hardcoded items. I'm writing this to compare a list of user input addresses to a unique items list of address pulled from a county address list. using the street names I'm using difflib to find the closest matching correct address to clean up common typos, incorrect road designations and formats. I can't figure out why this isn't writing correctly. If you can help me that would be great. The output doesn't need to be.txt. that is just what I was using to practice.

This seems like a simple mistake but I can't figure it out. When running the print statement in my IDE it comes out perfect: OBJECTID, LONG, LAT, ADDRESS

1,-121.5013397,38.57353936,624 Q ST
2,-121.4889809,38.58067826,1229 I ST
3,-121.6252964,38.68504066,7208 W ELKHORN BLVD
4,-121.4648967,38.57105638,3145 GRANADA WY
5,-121.5034945,38.56493704,731 BROADWAY
6,-121.4643582,38.54432866,3301 MARTIN LUTHER KING JR BLVD
7,-121.4267998,38.46806583,6500 WYNDHAM DR
8,-121.4277157,38.56776765,5990 H ST
9,-121.4261309,38.52390186,5642 66TH ST
10,-121.5312586,38.49791376,785 FLORIN ROAD
11,-121.4836172,38.53385557,4500 24TH ST
12,-121.5182376,38.51647637,1100 43RD AV
13,-121.4826673,38.59115124,1341 N C STREET
14,-121.497416,38.615358,1640 W EL CAMINO
15,-121.4798681,38.49076918,7363 24TH ST
16,-121.435397,38.64776157,1311 BELL AVE
17,-121.435397,38.64776157, 
18,-121.479827,38.64700504,746 NORTH MARKET BLVD
19,-121.4275146,38.59602966,1700 CHALLENGE WY
20,-121.4476495,38.61318471,2512 RIO LINDA BLVD
21,-121.5036868,38.67119467,1901 CLUB CENTER DR
22,-121.54029,38.6446808,4201 EL CENTRO RD
23,-121.4656495,38.51005465,3720 47TH AVE
24,-121.4538398,38.48538997,7927 EAST PARKWAY
25,-121.3928243,38.54872313,3301 JULLIARD DR
26,-121.4656495,38.51005465, 

and in the.txt it writes to, all I get is this:

26,-121.4656495,38.51005465, 

Here is what I have:

import csv
import usaddress
import difflib


def cls_name(stname, list):
    c = difflib.get_close_matches(
        stname,
        list)[0]
    return str(c)


unqlst = ['Q ST', 'I ST', 'W ELKHORN BLVD', 'GRANADA WY', 'BROADWAY', 'MARTIN LUTHER KING JR BLVD', 'WYNDHAM DR',
          'H ST', '66TH ST', 'FLORIN ROAD', '24TH ST', '43RD AV', 'N C STREET', 'W EL CAMINO', '24TH ST', 'BELL AVE',
          'NORTH MARKET BLVD', 'CHALLENGE WY', 'RIO LINDA BLVD', 'CLUB CENTER DR', 'EL CENTRO RD', '47TH AVE',
          'EAST PARKWAY']

# path = r'C:\Users\Michael\Desktop\Fire_Stations.csv'
# with open(path) as file:
with open(r"C:\Users\Michael\Desktop\Fire_Stations.csv") as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    # the below statement will skip the first row
    next(csv_reader)
    for line in csv_file:
        line = line.split(',')
        addys = line[3]
        # addys = addys.strip('\n')
        addys = addys.upper()
        addys = usaddress.tag(addys)  # prototype: getting parcel address w/o numbers for phase one cleaning, only
        try:
            rdnum = addys[0]['AddressNumber']  # Needed Try/Except I think because first title line
        except KeyError:
            rdnum = ''
        try:
            rsdir = addys[0]['StreetNamePreDirectional']
        except KeyError:
            rsdir = ''
        try:
            rdname = addys[0]['StreetName']
        except KeyError:
            rdname = ''
        try:
            rddsg = addys[0]['StreetNamePostType']
        except KeyError:
            rddsg = ''
        wrdsrdname = (rsdir, rdname, rddsg)
        wrdsrdname = " ".join(wrdsrdname)
        wrdsrdname = wrdsrdname.strip()

        try:
            if wrdsrdname in unqlst:  # if roadname is in the unique list from counties file, do nothing, if not find closest in list
                # print('ADDRESS CORRECT')
                pass  # print(rdname)
            else:
                wrdsrdname = cls_name(wrdsrdname, unqlst)  # calling fuction to find closest name
                # print('ADDRESS INCORRECT BUT FIXED')
            # print(rdname)
        except:
            # print('error002: no address match')
            pass

        tgthr = (rdnum, wrdsrdname)
        final = (' '.join(tgthr))
        # print(final)

        # header = ['OBJECTID', 'LONG', 'LAT', 'ADDRESS']

        data = [line[0], line[1], line[2], final]
        data = ','.join(data)
        print(data)

with open('Fire_Stations.txt', 'w') as f:
    f.write(data)

Because you created the data inside a loop, therefore it prints them line by line. Plus, it only remembers the last state of data , we just have to use "more global" like all_data instead.

all_data = ''
for line in csv_file:
    # your code goes here
    data = [line[0], line[1], line[2], final]
    data = ','.join(data)
    all_data += data + '\n'
    print(data)
    
with open('Fire_Stations.txt', 'w') as f:
    f.write(all_data)

This is because print is in for loop and write is not, to write all data in file you need to open the file before for loop and append data in file in for loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM