如果您知道这两个文本文件将具有相同顺序的完全相同的行:
from pathlib import Path
population_path = Path('population.txt')
area_path = Path('area.txt')
output_path = Path('density.txt')
with open(population_path, 'r') as pop_file, open(area_path, 'r') as area_file, open(output_path, 'w') as output_file:
for pop_line, area_line in zip(pop_file, area_file):
city_state_pop, population = pop_line.rsplit(' ', 1)
city_state_area, area = area_line.rsplit(' ', 1)
density = int(population) / int(area)
output_file.write(f"{city_state_pop} {density:.2f}\n")
Output 100:
Calico, California 6.00
Rhyolite, Nevada 1.00
Goldfield, Arizona 2.50
St. Elmo, Colorado 1.50
Terlingua, Texas 10.00
Virginia City, Montana 0.67
Nevada City, Montana 2.00
Bodie, California 0.45
Cahawba, Alabama 1.10
Kennecott, Alaska 0.01
Bannack, Montana 10.13
South Pass City, Wyoming 14.33
Centralia, Pennsylvania 1.00
Thurmond, West Virginia 1.10
Glenrio, Texas 1.00
Glenrio, New Mexico 1.00
Bulowville, Florida 0.90
或者,如果您无法保证两个文本文件中的顺序相同,请考虑将您的任务分解为步骤,并 for each 步骤创建助手函数:
from pathlib import Path
def read_data(filename: Path) -> dict[str, int]:
"""Reads data from a given filename and parses it into a dictionary.
Expected file only has lines with 'City, State' followed by whitespace and a integer.
Args:
filename (Path): The path to the file to read.
Returns:
A dictionary where keys are 'City, State' and values are integers.
"""
data = {}
with open(filename, 'r') as file:
for line in file:
line = line.strip()
if line:
parts = line.rsplit(' ', 1)
name = parts[0].strip()
number = int(parts[1])
data[name] = number
return data
def calculate_density(population_data: dict[str, int],
area_data: dict[str, int]) -> dict[str, float]:
"""Calculates the population density by dividing the population by the area.
Args:
population_data: A dictionary of populations keyed by 'City, State'.
area_data: A dictionary of areas keyed by 'City, State'.
Returns:
A dictionary of population densities keyed by 'City, State'.
"""
return {
city_state: population / area_data[city_state]
for city_state, population in population_data.items()
if city_state in area_data
}
def write_data(filename: Path, data: dict[str, float]) -> None:
"""Writes data dict to a file after sorting, with each key-value pair on a new line.
Args:
filename: The path to the file where data will be written.
data: The data to write, where each key-value pair is a line.
"""
longest_key_length = max(len(key) for key in data)
format_string = f"{{:<{longest_key_length}}} {{:>10}}\n"
with open(filename, 'w') as file:
for key, value in sorted(data.items()):
# Right-align the value in a 10-character wide field.
file.write(format_string.format(key, f"{value:.2f}"))
def main() -> None:
"""Entry point of script."""
write_data(filename=Path('density.txt'),
data=calculate_density(
population_data=read_data(Path('population.txt')),
area_data=read_data(Path('area.txt')),
))
if __name__ == '__main__':
main()
Output 100:
Bannack, Montana 10.13
Bodie, California 0.45
Bulowville, Florida 0.90
Cahawba, Alabama 1.10
Calico, California 6.00
Centralia, Pennsylvania 1.00
Glenrio, New Mexico 1.00
Glenrio, Texas 1.00
Goldfield, Arizona 2.50
Kennecott, Alaska 0.01
Nevada City, Montana 2.00
Rhyolite, Nevada 1.00
South Pass City, Wyoming 14.33
St. Elmo, Colorado 1.50
Terlingua, Texas 10.00
Thurmond, West Virginia 1.10
Virginia City, Montana 0.67