简体   繁体   English

Python:打印语句显示所有行,但仅将最后一行写入文件

[英]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.使用街道名称,我正在使用 difflib 查找最匹配的正确地址,以清理常见的拼写错误、不正确的道路名称和格式。 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. output 不需要是.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在我的 IDE 中运行 print 语句时,结果很完美: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:在它写入的.txt 中,我得到的只是:

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.因为您在循环中创建了data ,所以它会逐行打印它们。 Plus, it only remembers the last state of data , we just have to use "more global" like all_data instead.另外,它只记得最后一个 state 的data ,我们只需要使用像all_data这样的“更全局”来代替。

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.这是因为 print 在 for 循环中而 write 不是,要在文件中写入所有数据,您需要在 for 循环之前打开文件,并在 for 循环中打开文件中的 append 数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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