我复制了您的输入,在CSV中的每一行之前添加了换行符(参见
here
),及
将字典的初始化移动到循环中
.运行以下代码:
from pprint import pprint
def readParkFile(fileName="temp.csv"):
f = open(fileName, "r")
parkList = []
header = f.readline().split(",")
keys = list(header)
for line in f:
parkDict = {} # This is the issue! Moving the initialization of this dictionary into the loop.
tmp = list(line.split(","))
quote = tmp[7:]
fullQuote = ','.join(quote)
del tmp[7:]
tmp.append(fullQuote)
for value in tmp:
parkDict[keys[tmp.index(value)]] = value
# pprint(parkDict)
parkList.append(parkDict)
# pprint(parkList)
if __name__ == '__main__':
readParkFile()
您将得到以下输出:
[{'Acres': '47390',
'Code': 'ACAD',
'Date': '1919-02-26',
'Description \n': '"Covering most of Mount Desert Island and other coastal '
'islands, Acadia features the tallest mountain on the '
'Atlantic coast of the United States, granite peaks, ocean '
'shoreline, woodlands, and lakes. There are freshwater, '
'estuary, forest, and intertidal habitats." \n',
'Latitude': '44.35',
'Longitude': '-68.21',
'Name': 'Acadia National Park',
'State': 'ME'},
{'Acres': '76519',
'Code': 'ARCH',
'Date': '1971-11-12',
'Description \n': '"This site features more than 2\\,000 natural sandstone '
'arches, with some of the most popular arches in the park '
'being Delicate Arch, Landscape Arch and Double Arch. '
'Millions of years of erosion have created these '
'structures in a desert climate where the arid ground has '
'life-sustaining biological soil crusts and potholes that '
'serve as natural water-collecting basins. Other geologic '
'formations include stone pinnacles, fins, and balancing '
'rocks." \n',
'Latitude': '38.68',
'Longitude': '-109.57',
'Name': 'Arches National Park',
'State': 'UT'},
{'Acres': '242756',
'Code': 'BADL',
'Date': '1978-11-10',
'Description \n': '"The Badlands are a collection of buttes, pinnacles, '
'spires, and mixed-grass prairies. The White River '
'Badlands contain the largest assemblage of known late '
'Eocene and Oligocene mammal fossils. The wildlife '
'includes bison, bighorn sheep, black-footed ferrets, and '
'prairie dogs."\n',
'Latitude': '43.75',
'Longitude': '-102.5',
'Name': 'Badlands National Park',
'State': 'SD'},
{'Acres': '801163',
'Code': 'BIBE',
'Date': '1944-06-12',
'Description \n': '"Named for the prominent bend in the Rio Grande along the '
'U.S.-Mexico border, this park encompasses a large and '
'remote part of the Chihuahuan Desert. Its main attraction '
'is backcountry recreation in the arid Chisos Mountains '
'and in canyons along the river. A wide variety of '
'Cretaceous and Tertiary fossils as well as cultural '
'artifacts of Native Americans also exist within its '
'borders." \n',
'Latitude': '29.25',
'Longitude': '-103.25',
'Name': 'Big Bend National Park',
'State': 'TX'},
{'Acres': '172924',
'Code': 'BISC',
'Date': '1980-06-28',
'Description \n': '"The central part of Biscayne Bay, this mostly underwater '
'park at the north end of the Florida Keys has four '
'interrelated marine ecosystems: mangrove forest, the Bay, '
'the Keys, and coral reefs. Threatened animals include the '
'West Indian manatee, American crocodile, various sea '
'turtles, and peregrine falcon." \n',
'Latitude': '25.65',
'Longitude': '-80.08',
'Name': 'Biscayne National Park',
'State': 'FL'},
{'Acres': '32950',
'Code': 'BLCA',
'Date': '1999-10-21',
'Description \n': '"The park protects a quarter of the Gunnison River, which '
'slices sheer canyon walls from dark Precambrian-era rock. '
'The canyon features some of the steepest cliffs and '
'oldest rock in North America, and is a popular site for '
'river rafting and rock climbing. The deep, narrow canyon '
'is composed of gneiss and schist, which appears black '
'when in shadow." \n',
'Latitude': '38.57',
'Longitude': '-107.72',
'Name': 'Black Canyon of the Gunnison National Park',
'State': 'CO'},
{'Acres': '35835',
'Code': 'BRCA',
'Date': '1928-02-25',
'Description \n': '"Bryce Canyon is a geological amphitheater on the '
'Paunsaugunt Plateau with hundreds of tall, multicolored '
'sandstone hoodoos formed by erosion. The region was '
'originally settled by Native Americans and later by '
'Mormon pioneers." \n',
'Latitude': '37.57',
'Longitude': '-112.18',
'Name': 'Bryce Canyon National Park',
'State': 'UT'},
{'Acres': '337598',
'Code': 'CANY',
'Date': '1964-09-12',
'Description \n': '"This landscape was eroded into a maze of canyons, '
'buttes, and mesas by the combined efforts of the Colorado '
'River, Green River, and their tributaries, which divide '
'the park into three districts. The park also contains '
'rock pinnacles and arches, as well as artifacts from '
'Ancient Pueblo peoples." \n',
'Latitude': '38.2',
'Longitude': '-109.93',
'Name': 'Canyonlands National Park',
'State': 'UT'},
{'Acres': '241904',
'Code': 'CARE',
'Date': '1971-12-18',
'Description \n': '"The park\'s Waterpocket Fold is a 100-mile (160 km) '
"monocline that exhibits the earth's diverse geologic "
'layers. Other natural features include monoliths, cliffs, '
'and sandstone domes shaped like the United States '
'Capitol." \n',
'Latitude': '38.2',
'Longitude': '-111.17',
'Name': 'Capitol Reef National Park',
'State': 'UT'},
{'Acres': '46766',
'Code': 'CAVE',
'Date': '1930-05-14',
'Description \n': '"Carlsbad Caverns has 117 caves, the longest of which is '
'over 120 miles (190 km) long. The Big Room is almost '
'4,000 feet (1,200 m) long, and the caves are home to over '
'400,000 Mexican free-tailed bats and sixteen other '
'species. Above ground are the Chihuahuan Desert and '
'Rattlesnake Springs." \n',
'Latitude': '32.17',
'Longitude': '-104.44',
'Name': 'Carlsbad Caverns National Park',
'State': 'NM'},
{'Acres': '249561',
'Code': 'CHIS',
'Date': '1980-03-05',
'Description \n': '"Five of the eight Channel Islands are protected, with '
"half of the park's area underwater. The islands have a "
'unique Mediterranean ecosystem originally settled by the '
'Chumash people. They are home to over 2,000 species of '
'land plants and animals, 145 endemic to them, including '
'the island fox. Ferry services offer transportation to '
'the islands from the mainland." \n',
'Latitude': '34.01',
'Longitude': '-119.42',
'Name': 'Channel Islands National Park',
'State': 'CA'},
{'Acres': '26546',
'Code': 'CONG',
'Date': '2003-11-10',
'Description \n': '"On the Congaree River, this park is the largest portion '
'of old-growth floodplain forest left in North America. '
'Some of the trees are the tallest in the eastern United '
'States. An elevated walkway called the Boardwalk Loop '
'guides visitors through the swamp." \n',
'Latitude': '33.78',
'Longitude': '-80.78',
'Name': 'Congaree National Park',
'State': 'SC'},
{'Acres': '183224',
'Code': 'CRLA',
'Date': '1902-05-22',
'Description \n': '"Crater Lake lies in the caldera of an ancient volcano '
'called Mount Mazama that collapsed 7,700 years ago. The '
'lake is the deepest in the United States and is noted for '
'its vivid blue color and water clarity. Wizard Island and '
'the Phantom Ship are more recent volcanic formations '
'within the caldera. As the lake has no inlets or outlets, '
'it is replenished only by precipitation." \n',
'Latitude': '42.94',
'Longitude': '-122.1',
'Name': 'Crater Lake National Park',
'State': 'OR'},
{'Acres': '32950',
'Code': 'CUVA',
'Date': '2000-10-11',
'Description \n': '"This park along the Cuyahoga River has waterfalls, '
'hills, trails, and exhibits on early rural living. The '
'Ohio and Erie Canal Towpath Trail follows the Ohio and '
'Erie Canal, where mules towed canal boats. The park has '
'numerous historic homes, bridges, and structures, and '
'also offers a scenic train ride."\n',
'Latitude': '41.24',
'Longitude': '-81.55',
'Name': 'Cuyahoga Valley National Park',
'State': 'OH'}]