[英]Python: Print statement shows all lines but writes only last line to file
這是我的帶有硬編碼項目的概念證明腳本。 我寫這篇文章是為了將用戶輸入地址列表與從縣地址列表中提取的地址的唯一項目列表進行比較。 使用街道名稱,我正在使用 difflib 查找最匹配的正確地址,以清理常見的拼寫錯誤、不正確的道路名稱和格式。 我不明白為什么這寫得不正確。 如果你能幫助我,那就太好了。 output 不需要是.txt。 這正是我用來練習的。
這似乎是一個簡單的錯誤,但我無法弄清楚。 在我的 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,
在它寫入的.txt 中,我得到的只是:
26,-121.4656495,38.51005465,
這是我所擁有的:
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)
因為您在循環中創建了data
,所以它會逐行打印它們。 另外,它只記得最后一個 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)
這是因為 print 在 for 循環中而 write 不是,要在文件中寫入所有數據,您需要在 for 循環之前打開文件,並在 for 循環中打開文件中的 append 數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.