[英]Reading data from two txt file and writing related informations to a third txt file
編寫一個程序,讀取兩個國家數據文件worldpop.txt
和worldarea.txt
。 這兩個文件以相同的順序包含相同的國家/地區。 編寫一個包含國家名稱和人口密度(每平方公里人口)的文件density.txt
。
世界流行音樂.txt:
China 1415045928
India 1354051854
U.S. 326766748
Indonesia 266794980
Brazil 210867954
Pakistan 200813818
Nigeria 195875237
Bangladesh 166368149
Russia 143964709
Mexico 130759074
Japan 127185332
Ethiopia 107534882
Philippines 106512074
Egypt 99375741
Viet-Nam 96491146
DR-Congo 84004989
Germany 82293457
Iran 82011735
Turkey 81916871
Thailand 69183173
U.K. 66573504
France 65233271
Italy 59290969
世界區域.txt:
China 9388211
India 2973190
U.S. 9147420
Indonesia 1811570
Brazil 8358140
Pakistan 770880
Nigeria 910770
Bangladesh 130170
Russia 16376870
Mexico 1943950
Japan 364555
Ethiopia 1000000
Philippines 298170
Egypt 995450
Viet-Nam 310070
DR-Congo 2267050
Germany 348560
Iran 1628550
Turkey 769630
Thailand 510890
U.K. 241930
France 547557
Italy 294140
density.txt 應該是這樣的:
China 150.7258334947947
India 455.42055973550293
U.S. 35.72228540943785
Indonesia 147.27279652456156
Brazil 25.22905263611282
Pakistan 260.49945257368205
Nigeria 215.06553465748763
Bangladesh 1278.0836521471922
Russia 8.790734065789128
Mexico 67.26462820545795
Japan 348.8783091714556
Ethiopia 107.534882
Philippines 357.2192843009022
Egypt 99.8299673514491
Viet-Nam 311.1914922436869
DR-Congo 37.054757945347475
Germany 236.0955273123709
Iran 50.35874550980934
Turkey 106.43669165703
Thailand 135.41696451290883
U.K. 275.1767205389989
France 119.13512383185677
Italy 201.57397497790168
我寫的程序:
f=open('worldpop.txt','r')
f2=open('worldarea.txt','r')
out=open('density.txt','w')
for line1 in f: #finding country names
pos1=line1.find(' ')
country=line1[0:pos1]+'\n'
for line2 in f: #finding population numbers
pos2=line2.find(' ')
population=line2[pos2+1:]
for line3 in f2: #finding area numbers
pos3=line3.find(' ')
area=line3[pos3+1:]
for line1 in f: #writing density to a new file
density=population/area
out.write(density)
out.close()
f.close()
f2.close()
當我運行程序時,密度.txt 是空的。 我該如何解決這個問題? 謝謝。 注意:我知道它的替代解決方案,但我主要想用這種方法解決它,所以請不要使用其他方法。
文件為 stream。 他們得到了一個“指針”,指向它們在 stream 中的位置。 如果您讀到最后,除非您關閉/重新打開 stream 或尋找更早的 position => 再次迭代f
不起作用,否則您將無法再次閱讀。
你不需要那個,因為你的國家以相同的順序出現。 您可以簡單地讀取填充文件(一行),在末尾去除換行符,然后從區域文件中讀取一行並提取區域 - 然后將兩者放在一起並添加換行符並將其寫入 output。
例子:
with open('worldpop.txt','w') as r:
r.write("""China 9388211
India 2973190
U.S. 9147420""")
with open('worldarea.txt','w') as f2:
f2.write("""China 150.7258334947947
India 455.42055973550293
U.S. 35.72228540943785""")
with open('worldpop.txt') as r, open('worldarea.txt') as f2, open('d.txt','w') as out:
for line1 in r: #finding country names
l = line1.strip()
area = next(f2).split()[-1] # next(.) reads one line for that file
out.write(f"{l} {area}\n")
with open("d.txt") as d:
print(d.read())
Output 組合文件:
China 9388211 150.7258334947947
India 2973190 455.42055973550293
U.S. 9147420 35.72228540943785
如果要進行計算,則需要將數字字符串轉換為數字:
with open('worldpop.txt','r') as r, open(
'worldarea.txt','r') as f2, open('density.txt','w') as out:
for line1 in r: #finding country names
l = line1.strip()
ppl = float(l.strip().split()[-1]) # only use the number, convert to float
area = float(next(f2).split()[-1]) # only use the number, convert to float
# how many ppl per 1 area?
d = ppl / area
out.write(f"{l} {area} {d:f}\n")
with open("density.txt") as d:
print(d.read())
要獲得多少人/地區的生活:
China 9388211 150.7258334947947 62286.674967
India 2973190 455.42055973550293 6528.449224
U.S. 9147420 35.72228540943785 256070.402416
您的代碼沒有進入最后一個循環,因為此時f
迭代器已經用盡(您只能在每個迭代器上迭代一次,並且您已經在第一個循環中完成了它)。
嘗試以只迭代每個文件一次的方式更改代碼。
順便說一句#1 - 同樣的問題發生在你的第二個循環中。
順便說一句#2 - 您應該將提取的人口和面積值轉換為數字類型(在您的情況下為float
),以便將它們分開以找到密度。
這是您的功能和測試代碼:
pop = open('worldpop.txt', 'r') #r is for read
area = open('worldarea.txt', 'r')
out = open('density.txt', 'w')
poplist = pop.readlines() #will read the file and put each line into a list (new element)
arealist = area.readlines()
output = []
for i in range(len(poplist)):
poplist[i] = poplist[i].split(" ")
#slices the elements into lists, so we get a list of lists
poplist[i][1] = poplist[i][1].replace("\n", "")
for i in range(len(arealist)):
arealist[i] = arealist[i].split(" ")
arealist[i][1] = arealist[i][1].replace("\n", "")
#two indexes : we're in a list of lists
for i in range(len(poplist)):
out.write(poplist[i][0]+" ")
out.write(str(int(poplist[i][1])/int(arealist[i][1]))+"\n")
out.close()
pop.close()
area.close()
它可以工作,而不是使用循環來讀取行。 我只是使用“readlines”讓一切變得更簡單。 然后我只需要清除列表並獲取輸出。
你可以試試這個
定義文件命名變量
WORLD_POP_PATH = 'worldpop.txt'
WORLD_AREA_PATH = 'worldarea.txt'
OUTPUT_PATH = 'density.txt'
與其對每個國家使用文件 position,不如使用以國家名稱作為鍵的字典更有效。
def generate_dict(file_path):
with open(file_path, 'r') as f_world_pop:
split_line = lambda x: x.split()
return {
line[0]: line[1]
for line in
map(split_line, f_world_pop.readlines())
}
我們用檔案的內容分別生成世界人口和世界區域字典
world_pop = generate_dict( WORLD_POP_PATH )
world_area = generate_dict( WORLD_AREA_PATH )
我們生成一個包含名稱和流行的密度列表。 密度
density = [f'{country} {int(world_pop[country])/int(world_area[country])}'
for country in world_pop.keys() ]
最后,我們將結果寫入 output 文件
with open(OUTPUT_PATH, 'w+') as f_dens:
f_dens.write('\n'.join(density))
應該更喜歡使用with
而不是open
關於with
: python 關鍵字“with”用於什么?
next
方法將遍歷 object 而不是將所有文件內容加載到 memory
with open("./worldpop.txt") as f, open("./worldarea.txt") as f2, open("./density.txt","w") as out:
for line in f:
country, pop = line.split(" ")
_, area = next(f2).split(" ")
out.writelines(f"{country} {int(pop)/int(area)}\n")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.