将文件写入Python 3

2024-06-14 06:43:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,如有任何指导,将不胜感激

我正在写入PC.txt文件,但无法写入所需的所有信息

资料来源:

countries_and_capitals = (['Afghanistan', 'Kabul'], ['Albania', 'Tirana (Tirane)'], ['Algeria', 'Algiers'], ['Andorra', 'Andorra la Vella'], ['Angola', 'Luanda'], ['Antigua and Barbuda', "Saint John's"], ['Argentina', 'Buenos Aires'], ['Armenia', 'Yerevan'], ['Australia', 'Canberra'], ['Austria', 'Vienna'], ['Azerbaijan', 'Baku'], ['Bahamas', 'Nassau'], ['Bahrain', 'Manama'], ['Bangladesh', 'Dhaka'], ['Barbados', 'Bridgetown'], ['Belarus', 'Minsk'], ['Belgium', 'Brussels'], ['Belize', 'Belmopan'], ['Benin', 'Porto Novo'], ['Bhutan', 'Thimphu'], ['Bolivia', 'Sucre'], ['Bosnia and Herzegovina', 'Sarajevo'], ['Botswana', 'Gaborone'], ['Brazil', 'Brasilia'], ['Brunei', 'Bandar Seri Begawan'], ['Bulgaria', 'Sofia'], ['Burkina Faso', 'Ouagadougou'], ['Burundi', 'Gitega'], ['Cambodia', 'Phnom Penh'], ['Cameroon', 'Yaounde'], ['Canada', 'Ottawa'], ['Cape Verde', 'Praia'], ['Central African Republic', 'Bangui'], ['Chad', "N'Djamena"], ['Chile', 'Santiago'], ['China', 'Beijing'], ['Colombia', 'Bogota'], ['Comoros', 'Moroni'], ['Congo, Democratic Republic of the', 'Kinshasa'], ['Congo, Republic of the', 'Brazzaville'], ['Costa Rica', 'San Jose'], ["Cote d'Ivoire (Ivory Coast)", 'Yamoussoukro'], ['Croatia', 'Zagreb'], ['Cuba', 'Havana'], ['Cyprus', 'Nicosia'], ['Czech Republic (Czechia)', 'Prague'], ['Denmark', 'Copenhagen'], ['Djibouti', 'Djibouti'], ['Dominica', 'Roseau'], ['Dominican Republic', 'Santo Domingo'], ['East Timor', 'Dili'], ['Ecuador', 'Quito'], ['Egypt', 'Cairo'], ['El Salvador', 'San Salvador'], ['England', 'London'], ['Equatorial Guinea', 'Malabo'], ['Eritrea', 'Asmara'], ['Estonia', 'Tallinn'], ['Eswatini (Swaziland)', 'Mbabana'], ['Ethiopia', 'Addis Ababa'], ['Federated States of Micronesia', 'Palikir'], ['Fiji', 'Suva'], ['Finland', 'Helsinki'], ['France', 'Paris'], ['Gabon', 'Libreville'], ['Gambia', 'Banjul'], ['Georgia', 'Tbilisi'], ['Germany', 'Berlin'], ['Ghana', 'Accra'], ['Greece', 'Athens'], ['Grenada', "Saint George's"], ['Guatemala', 'Guatemala City'], ['Guinea', 'Conakry'], ['Guinea-Bissau', 'Bissau'], ['Guyana', 'Georgetown'], ['Haiti', 'Port au Prince'], ['Honduras', 'Tegucigalpa'], ['Hungary', 'Budapest'], ['Iceland', 'Reykjavik'], ['India', 'New Delhi'], ['Indonesia', 'Jakarta'], ['Iran', 'Tehran'], ['Iraq', 'Baghdad'], ['Ireland', 'Dublin'], ['Israel', 'Jerusalem'], ['Italy', 'Rome'], ['Jamaica', 'Kingston'], ['Japan', 'Tokyo'], ['Jordan', 'Amman'], ['Kazakhstan', 'Nur-Sultan'], ['Kenya', 'Nairobi'], ['Kiribati', 'Tarawa Atoll'], ['Kosovo', 'Pristina'], ['Kuwait', 'Kuwait City'], ['Kyrgyzstan', 'Bishkek'], ['Laos', 'Vientiane'], ['Latvia', 'Riga'], ['Lebanon', 'Beirut'], ['Lesotho', 'Maseru'], ['Liberia', 'Monrovia'], ['Libya', 'Tripoli'], ['Liechtenstein', 'Vaduz'], ['Lithuania', 'Vilnius'], ['Luxembourg', 'Luxembourg'], ['Madagascar', 'Antananarivo'], ['Malawi', 'Lilongwe'], ['Malaysia', 'Kuala Lumpur'], ['Maldives', 'Male'], ['Mali', 'Bamako'], ['Malta', 'Valletta'], ['Marshall Islands', 'Majuro'], ['Mauritania', 'Nouakchott'], ['Mauritius', 'Port Louis'], ['Mexico', 'Mexico City'], ['Moldova', 'Chisinau'], ['Monaco', 'Monaco'], ['Mongolia', 'Ulaanbaatar'], ['Montenegro', 'Podgorica'], ['Morocco', 'Rabat'], ['Mozambique', 'Maputo'], ['Myanmar (Burma)', 'Nay Pyi Taw'], ['Namibia', 'Windhoek'], ['Nauru', 'No official capital'], ['Nepal', 'Kathmandu'], ['Netherlands', 'Amsterdam'], ['New Zealand', 'Wellington'], ['Nicaragua', 'Managua'], ['Niger', 'Niamey'], ['Nigeria', 'Abuja'], ['North Korea', 'Pyongyang'], ['North Macedonia (Macedonia)', 'Skopje'], ['Northern Ireland', 'Belfast'], ['Norway', 'Oslo'], ['Oman', 'Muscat'], ['Pakistan', 'Islamabad'], ['Palau', 'Melekeok'], ['Panama', 'Panama City'], ['Papua New Guinea', 'Port Moresby'], ['Paraguay', 'Asuncion'], ['Peru', 'Lima'], ['Philippines', 'Manila'], ['Poland', 'Warsaw'], ['Portugal', 'Lisbon'], ['Qatar', 'Doha'], ['Romania', 'Bucharest'], ['Russia', 'Moscow'], ['Rwanda', 'Kigali'], ['Saint Kitts and Nevis', 'Basseterre'], ['Saint Lucia', 'Castries'], ['Saint Vincent and the Grenadines', 'Kingstown'], ['Samoa', 'Apia'], ['San Marino', 'San Marino'], ['Sao Tome and Principe', 'Sao Tome'], ['Saudi Arabia', 'Riyadh'], ['Scotland', 'Edinburgh'], ['Senegal', 'Dakar'], ['Serbia', 'Belgrade'], ['Seychelles', 'Victoria'], ['Sierra Leone', 'Freetown'], ['Singapore', 'Singapore'], ['Slovakia', 'Bratislava'], ['Slovenia', 'Ljubljana'], ['Solomon Islands', 'Honiara'], ['Somalia', 'Mogadishu'], ['South Africa', 'Pretoria, Bloemfontein, Cape Town'], ['South Korea', 'Seoul'], ['South Sudan', 'Juba'], ['Spain', 'Madrid'], ['Sri Lanka', 'Colombo'], ['Sudan', 'Khartoum'], ['Suriname', 'Paramaribo'], ['Sweden', 'Stockholm'], ['Switzerland', 'Bern'], ['Syria', 'Damascus'], ['Taiwan', 'Taipei'], ['Tajikistan', 'Dushanbe'], ['Tanzania', 'Dodoma'], ['Thailand', 'Bangkok'], ['Togo', 'Lome'], ['Tonga', "Nuku'alofa"], ['Trinidad and Tobago', 'Port of Spain'], ['Tunisia', 'Tunis'], ['Turkey', 'Ankara'], ['Turkmenistan', 'Ashgabat'], ['Tuvalu', 'Funafuti'], ['Uganda', 'Kampala'], ['Ukraine', 'Kiev'], ['United Arab Emirates', 'Abu Dhabi'], ['United Kingdom', 'London'], ['United States', 'Washington D.C.'], ['Uruguay', 'Montevideo'], ['Uzbekistan', 'Tashkent'], ['Vanuatu', 'Port Vila'], ['Vatican City', 'Vatican City'], ['Venezuela', 'Caracas'], ['Vietnam', 'Hanoi'], ['Wales', 'Cardiff'], ['Yemen', "Sana'a"], ['Zambia', 'Lusaka'], ['Zimbabwe', 'Harare'])

我的代码:

for province, city in countries_and_capitals:
    print("The capital of %s is %s" % (province, city), file=open("PC", "w"))

写入PC.txt的内容:

The capital of Zimbabwe is Harare

我需要的是:

The capital of Afghanistan is Kabul
The capital of Albania is Tirana (Tirane)
The capital of Algeria is Algiers
The capital of Andorra is Andorra la Vella
The capital of Angola is Luanda
The capital of Antigua and Barbuda is Saint John's
The capital of Argentina is Buenos Aires
The capital of Armenia is Yerevan
The capital of Australia is Canberra
The capital of Austria is Vienna
The capital of Azerbaijan is Baku
The capital of Bahamas is Nassau
The capital of Bahrain is Manama
The capital of Bangladesh is Dhaka
The capital of Barbados is Bridgetown
The capital of Belarus is Minsk
The capital of Belgium is Brussels
The capital of Belize is Belmopan
The capital of Benin is Porto Novo
The capital of Bhutan is Thimphu
The capital of Bolivia is Sucre
The capital of Bosnia and Herzegovina is Sarajevo
The capital of Botswana is Gaborone
The capital of Brazil is Brasilia
The capital of Brunei is Bandar Seri Begawan
The capital of Bulgaria is Sofia
The capital of Burkina Faso is Ouagadougou
The capital of Burundi is Gitega
The capital of Cambodia is Phnom Penh
The capital of Cameroon is Yaounde
The capital of Canada is Ottawa
The capital of Cape Verde is Praia
The capital of Central African Republic is Bangui
The capital of Chad is N'Djamena
The capital of Chile is Santiago
The capital of China is Beijing
The capital of Colombia is Bogota
The capital of Comoros is Moroni
The capital of Congo, Democratic Republic of the is Kinshasa
The capital of Congo, Republic of the is Brazzaville
The capital of Costa Rica is San Jose
The capital of Cote d'Ivoire (Ivory Coast) is Yamoussoukro
The capital of Croatia is Zagreb
The capital of Cuba is Havana
The capital of Cyprus is Nicosia
The capital of Czech Republic (Czechia) is Prague
The capital of Denmark is Copenhagen
The capital of Djibouti is Djibouti
The capital of Dominica is Roseau
The capital of Dominican Republic is Santo Domingo
The capital of East Timor is Dili
The capital of Ecuador is Quito
The capital of Egypt is Cairo
The capital of El Salvador is San Salvador
The capital of England is London
The capital of Equatorial Guinea is Malabo
The capital of Eritrea is Asmara
The capital of Estonia is Tallinn
The capital of Eswatini (Swaziland) is Mbabana
The capital of Ethiopia is Addis Ababa
The capital of Federated States of Micronesia is Palikir
The capital of Fiji is Suva
The capital of Finland is Helsinki
The capital of France is Paris
The capital of Gabon is Libreville
The capital of Gambia is Banjul
The capital of Georgia is Tbilisi
The capital of Germany is Berlin
The capital of Ghana is Accra
The capital of Greece is Athens
The capital of Grenada is Saint George's
The capital of Guatemala is Guatemala City
The capital of Guinea is Conakry
The capital of Guinea-Bissau is Bissau
The capital of Guyana is Georgetown
The capital of Haiti is Port au Prince
The capital of Honduras is Tegucigalpa
The capital of Hungary is Budapest
The capital of Iceland is Reykjavik
The capital of India is New Delhi
The capital of Indonesia is Jakarta
The capital of Iran is Tehran
The capital of Iraq is Baghdad
The capital of Ireland is Dublin
The capital of Israel is Jerusalem
The capital of Italy is Rome
The capital of Jamaica is Kingston
The capital of Japan is Tokyo
The capital of Jordan is Amman
The capital of Kazakhstan is Nur-Sultan
The capital of Kenya is Nairobi
The capital of Kiribati is Tarawa Atoll
The capital of Kosovo is Pristina
The capital of Kuwait is Kuwait City
The capital of Kyrgyzstan is Bishkek
The capital of Laos is Vientiane
The capital of Latvia is Riga
The capital of Lebanon is Beirut
The capital of Lesotho is Maseru
The capital of Liberia is Monrovia
The capital of Libya is Tripoli
The capital of Liechtenstein is Vaduz
The capital of Lithuania is Vilnius
The capital of Luxembourg is Luxembourg
The capital of Madagascar is Antananarivo
The capital of Malawi is Lilongwe
The capital of Malaysia is Kuala Lumpur
The capital of Maldives is Male
The capital of Mali is Bamako
The capital of Malta is Valletta
The capital of Marshall Islands is Majuro
The capital of Mauritania is Nouakchott
The capital of Mauritius is Port Louis
The capital of Mexico is Mexico City
The capital of Moldova is Chisinau
The capital of Monaco is Monaco
The capital of Mongolia is Ulaanbaatar
The capital of Montenegro is Podgorica
The capital of Morocco is Rabat
The capital of Mozambique is Maputo
The capital of Myanmar (Burma) is Nay Pyi Taw
The capital of Namibia is Windhoek
The capital of Nauru is No official capital
The capital of Nepal is Kathmandu
The capital of Netherlands is Amsterdam
The capital of New Zealand is Wellington
The capital of Nicaragua is Managua
The capital of Niger is Niamey
The capital of Nigeria is Abuja
The capital of North Korea is Pyongyang
The capital of North Macedonia (Macedonia) is Skopje
The capital of Northern Ireland is Belfast
The capital of Norway is Oslo
The capital of Oman is Muscat
The capital of Pakistan is Islamabad
The capital of Palau is Melekeok
The capital of Panama is Panama City
The capital of Papua New Guinea is Port Moresby
The capital of Paraguay is Asuncion
The capital of Peru is Lima
The capital of Philippines is Manila
The capital of Poland is Warsaw
The capital of Portugal is Lisbon
The capital of Qatar is Doha
The capital of Romania is Bucharest
The capital of Russia is Moscow
The capital of Rwanda is Kigali
The capital of Saint Kitts and Nevis is Basseterre
The capital of Saint Lucia is Castries
The capital of Saint Vincent and the Grenadines is Kingstown
The capital of Samoa is Apia
The capital of San Marino is San Marino
The capital of Sao Tome and Principe is Sao Tome
The capital of Saudi Arabia is Riyadh
The capital of Scotland is Edinburgh
The capital of Senegal is Dakar
The capital of Serbia is Belgrade
The capital of Seychelles is Victoria
The capital of Sierra Leone is Freetown
The capital of Singapore is Singapore
The capital of Slovakia is Bratislava
The capital of Slovenia is Ljubljana
The capital of Solomon Islands is Honiara
The capital of Somalia is Mogadishu
The capital of South Africa is Pretoria, Bloemfontein, Cape Town
The capital of South Korea is Seoul
The capital of South Sudan is Juba
The capital of Spain is Madrid
The capital of Sri Lanka is Colombo
The capital of Sudan is Khartoum
The capital of Suriname is Paramaribo
The capital of Sweden is Stockholm
The capital of Switzerland is Bern
The capital of Syria is Damascus
The capital of Taiwan is Taipei
The capital of Tajikistan is Dushanbe
The capital of Tanzania is Dodoma
The capital of Thailand is Bangkok
The capital of Togo is Lome
The capital of Tonga is Nuku'alofa
The capital of Trinidad and Tobago is Port of Spain
The capital of Tunisia is Tunis
The capital of Turkey is Ankara
The capital of Turkmenistan is Ashgabat
The capital of Tuvalu is Funafuti
The capital of Uganda is Kampala
The capital of Ukraine is Kiev
The capital of United Arab Emirates is Abu Dhabi
The capital of United Kingdom is London
The capital of United States is Washington D.C.
The capital of Uruguay is Montevideo
The capital of Uzbekistan is Tashkent
The capital of Vanuatu is Port Vila
The capital of Vatican City is Vatican City
The capital of Venezuela is Caracas
The capital of Vietnam is Hanoi
The capital of Wales is Cardiff
The capital of Yemen is Sana'a
The capital of Zambia is Lusaka
The capital of Zimbabwe is Harare

Tags: andofthecitynewisportunited
2条回答

您可以尝试使用file=open(“PC”、“a”)而不是file=open(“PC”、“w”)以追加模式而不是写入模式打开文件

for province, city in countries_and_capitals:
    print("The capital of %s is %s" % (province, city), file=open("PC", "a"))

当您尝试以写入模式w将字符串写入文件时,它将在每个循环中反复覆盖现有文件,例如:

在第一个循环中,脚本将编写您给定的字符串,即“阿富汗首都是喀布尔”。但是,当您进入第二个循环时,它将覆盖到“阿尔巴尼亚首都是地拉那(地拉那)”。当脚本到达最后一次迭代时,它将覆盖字符串“津巴布韦首都是哈拉雷”,这是您得到的结果

您应该在模式a下使用文件,该模式将值写入文件末尾而不覆盖它。但通过这种方式,您可能希望在循环内部的字符串末尾添加“\n”(新行代码),以提高可读性。因此,您应该如下更新代码:

for province, city in countries_and_capitals:
    print("The capital of %s is %s" % (province, city), file=open("PC", "a"))

我还想指出,我不喜欢在Python中使用“%s”字符,因为它起源于C。有更多的pythonic方法可以做到这一点。如果您使用的是python2,则可以使用:

print("The capital of {} is {}".format(province, city), file=open("PC", "a"))

如果您使用的是python3,那么就可以使用属性'format()'和f字符串,这要干净得多。下面是f字符串的一个示例:

print(f"The capital of {province} is {city}", file=open("PC", "a"))

相关问题 更多 >