使用 Tkinter 读取 shapefile 产生错误
Read shapefile with Tkinter produce error
我正在尝试通过 Tkinter 和 tkFileDialog 加载和读取 shapefile。
以下代码显示了所做的工作。
首先我尝试直接读取 shapefile 并打印其内容 - 没有错误,然后使用 Tkinter 对话框读取相同的 shapefile 产生错误。
import shapefile
import Tkinter, tkFileDialog
# Read the Arena shapefile using the Reader class of the pyshp module
arenafile = r"data\Arenas_NBA\Arenas_NBA.shp"
arena_shapefile = shapefile.Reader(arenafile)
arena_shapes = arena_shapefile.shapes()
arena_records = arena_shapefile.records()
print arena_records
root = Tkinter.Tk()
root.withdraw()
arenafile_tk = tkFileDialog.askopenfile(parent=root,
mode='rb',
initialdir="data\Arenas_NBA",
title="Select Arena Shapefile",
filetypes=(("shapefiles", "*.shp"), ("all files", "*.*")))
if arenafile_tk != None:
arena_shapefile_tk = shapefile.Reader(arenafile_tk)
arena_shapes_tk = arena_shapefile_tk.shapes()
arena_records_tk = arena_shapefile_tk.records()
arenafile_tk.close()
print arena_records_tk
此代码产生以下输出:
[[u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20040211', u'20100215', u'', u'American Airlines Arena', u'', u'', u'601 Biscayne Boulevard', u'', u'', u'', u'Miami', u'FL', u'33132', u'', u'Dade', u'Miami-Fort Lauderdale', 4.0, 25.78117, -80.18758, u'High', u'NGA', u'', u'Eastern', u'Southeast', 19600.0, u'Miami Heat'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20040211', u'20100215', u'', u'ARCO Arena', u'', u'', u'1 Sports Parkway', u'', u'', u'', u'Sacramento', u'CA', u'95834', u'', u'Sacramento', u'Sacramento', 9.0, 38.64932, -121.51869, u'High', u'NGA', u'', u'Western', u'Pacific', 17317.0, u'Sacramento Kings'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20040211', u'20100215', u'', u'EnergySolutions Arena', u'', u'', u'301 West South Temple', u'', u'', u'', u'Salt Lake City', u'UT', u'84101', u'', u'Salt Lake', u'Salt Lake City-Ogden', 8.0, 40.76629, -111.90093, u'High', u'NGA', u'Shares arean with Thunder (AF1).', u'Western', u'Northwest', 19911.0, u'Utah Jazz'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20040226', u'20100215', u'', u'Fed Ex Forum', u'', u'', u'191 Beale Street', u'', u'', u'', u'Memphis', u'TN', u'38103', u'', u'Shelby', u'Memphis', 4.0, 35.1381, -90.0513, u'High', u'NGA', u'Shares arena with Memphis Tigers (NCAA).', u'Western', u'Southwest', 18119.0, u'Memphis Gizzlies'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Quicken Loans Arena', u'', u'', u'1 Center Court', u'', u'', u'', u'Cleveland', u'OH', u'44115', u'44115-4001', u'Cuyahoga', u'Cleveland-Akron', 5.0, 41.49648, -81.6887, u'High', u'NGA', u'Shares areana with Monsters (AHL) and Gladiators (AF1)', u'Eastern', u'Central', 20562.0, u'Cleveland Cavaliers'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Oracle Arena', u'', u'', u'7000 Coliseum Way', u'', u'', u'', u'Oakland', u'CA', u'94621', u'94621-1917', u'Alameda', u'San Francisco-Oakland', 9.0, 37.75131, -122.20217, u'High', u'NGA', u'', u'Western', u'Pacific', 19596.0, u'Golden State Warriors'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'New Orleans Arena', u'', u'', u'1601 Girod Street', u'', u'', u'', u'New Orleans', u'LA', u'70113', u'70113-3124', u'Orleans', u'New Orleans', 6.0, 29.94889, -90.08203, u'High', u'NGA', u'', u'Western', u'Southwest', 18500.0, u'New Orleans Hornets'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Rose Garden', u'', u'', u'1 Center Court', u'', u'', u'', u'Portland', u'OR', u'97227', u'', u'Multnomah', u'Portland', 10.0, 45.53165, -122.66692, u'High', u'NGA', u'Shares areana with Winterhawks (WHL).', u'Western', u'Northwest', 20630.0, u'Portland Trailblazers'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Amway Arena', u'', u'', u'600 West Amelia Street', u'', u'', u'', u'Orlando', u'FL', u'32801', u'', u'Orange', u'Orlando', 4.0, 28.54883, -81.38636, u'High', u'NGA', u'Shares arena with Titans (NLL) and Predators (AF1)', u'Eastern', u'Southeast', 17519.0, u'Orlando Magic'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Bradley Center', u'', u'', u'1001 North 4th Street', u'', u'', u'', u'Milwaukee', u'WI', u'53203', u'53203-1314', u'Milwaukee', u'Milwaukee', 5.0, 43.04346, -87.91736, u'High', u'NGA', u'Shares arena with Marquette Golden Eagles (NCAA)', u'Eastern', u'Central', 18717.0, u'Milwaukee Bucks'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'American Airlines Center', u'', u'', u'2500 Victory Avenue', u'', u'', u'', u'Dallas', u'TX', u'75219', u'75219-7601', u'Dallas', u'Dallas-Fort Worth', 6.0, 32.79062, -96.81043, u'High', u'NGA', u'Shares arena with Stars (NHL)..', u'Western', u'Southwest', 20337.0, u'Dallas Mavericks'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'TD Garden Center', u'', u'', u'100 Legends Way', u'', u'', u'', u'Boston', u'MA', u'02114', u'02114-4714', u'Suffolk', u'Boston', 1.0, 42.36653, -71.06206, u'High', u'NGA', u'Shares arena with Bruins (NHL) and Blazers (NLL).', u'Eastern', u'Atlantic', 18624.0, u'Boston Celtics'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Pepsi Center', u'', u'', u'1000 Chopper Circle', u'', u'', u'', u'Denver', u'CO', u'80204', u'', u'Denver', u'Denver', 8.0, 39.74916, -105.00677, u'High', u'NGA', u'Shares arena with Avalanche (NHL) and Mammoth (NLL).', u'Western', u'Northwest', 19155.0, u'Denver Nuggets'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Philips Arena', u'', u'', u'1 Philips Drive NW', u'', u'', u'', u'Atlanta', u'GA', u'30303', u'', u'Fulton', u'Atlanta', 4.0, 33.7573, -84.39631, u'High', u'NGA', u'Shares arena with Thashers (NHL) and Dream (WNBA).', u'Eastern', u'Southeast', 19445.0, u'Atlanta Hawks'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'United Center', u'', u'', u'1902 West Madison Street', u'', u'', u'', u'Chicago', u'IL', u'60612', u'60612-2459', u'Cook', u'Chicago', 5.0, 41.88059, -87.67418, u'High', u'NGA', u'Shares arena with Blackhawks (NHL).', u'Eastern', u'Central', 20917.0, u'Chicago Bulls'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Wachovia Center', u'', u'', u'3601 South Broad Street', u'', u'', u'', u'Philadelphia', u'PA', u'19148', u'', u'Philadelphia', u'Philadelphia', 3.0, 39.90097, -75.17218, u'High', u'NGA', u'Shares arena with Flyers (NHL) and Wings (NLL).', u'Eastern', u'Atlantic', 21600.0, u'Philadelphia 76ers'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'IZOD Center', u'', u'', u'50 Route 120', u'', u'', u'', u'East Rutherfor', u'NJ', u'07073', u'', u'Bergen', u'New York-Newark', 2.0, 40.81163, -74.06767, u'High', u'NGA', u'', u'Eastern', u'Atlantic', 20049.0, u'New Jersey Nets'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Madison Square Garden', u'', u'', u'4 Pennsylvania Plaza', u'', u'', u'', u'New York', u'NY', u'10001', u'', u'New York', u'New York-Newark', 2.0, 40.7506, -73.99378, u'High', u'NGA', u'Shares arena with Rangers (NHL), Saint John Red Storm (NCAA), and Liberty (WNBA).', u'Eastern', u'Atlantic', 19763.0, u'New York Knicks'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Verizon Center', u'', u'', u'601 F Street NW', u'', u'', u'', u'Washington', u'DC', u'20004', u'', u'District of Columbi', u'Washington', 3.0, 38.89838, -77.02098, u'High', u'NGA', u'Shares arena with Capitals (NHL), Mystics (WNBA) and Georgetown Hoyas (NCAA)', u'Eastern', u'Southeast', 20173.0, u'Washington Wizards'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Staples Center', u'', u'', u'1111 South Figueroa Street', u'', u'', u'', u'Los Angeles', u'CA', u'90015', u'', u'Los Angeles', u'Los Angeles', 9.0, 34.04321, -118.26716, u'High', u'NGA', u'Shares arena with Kings (NHL), Sparks (WNBA), and D-Fenders(D-League).', u'Western', u'Pacific', 18997.0, u'Los Angeles Lakers, Clipppers'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Target Center', u'', u'', u'600 First Avenue North', u'', u'', u'', u'Minneapolis', u'MN', u'55403', u'', u'Hennepin', u'Minneapolis-Saint Paul', 5.0, 44.97944, -93.27609, u'High', u'NGA', u'Shares arena with Lynx (WNBA).', u'Western', u'Northwest', 20500.0, u'Minnesota Timberwolves'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'US Airways Center', u'', u'', u'201 East Jefferson', u'', u'', u'', u'Phoenix', u'AZ', u'85004', u'85004-2412', u'Maricopa', u'Phoenix', 8.0, 33.446, -112.07125, u'High', u'NGA', u'Shares arena with Mercury (WNBA), Rattlers (AF1)', u'Western', u'Pacific', 18422.0, u'Phoenix Suns'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Time Warner Cable Arena', u'', u'', u'333 East Trade Street', u'', u'', u'', u'Charlotte', u'NC', u'28202', u'', u'Mecklenburg', u'Charlotte', 4.0, 35.22373, -80.83999, u'High', u'NGA', u'Shares arena with Checkers (AHL),', u'Eastern', u'Southeast', 19026.0, u'Charlotte Bobcats'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Conseco Fieldhouse', u'', u'', u'125 South Pennsylvania Street', u'', u'', u'', u'Indianapolis', u'IN', u'46204', u'', u'Marion', u'Indianapolis', 5.0, 39.76401, -86.15557, u'High', u'NGA', u'Shares arena with Fever (WNBA).', u'Eastern', u'Central', 18345.0, u'Indiana Pacers'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Toyota Center (Houston)', u'', u'', u'1510 Polk Street', u'', u'', u'', u'Houston', u'TX', u'77002', u'77002-7130', u'Harris', u'Houston', 6.0, 29.75078, -95.36218, u'High', u'NGA', u'Shares arena with Comets (WNBA).', u'Western', u'Southwest', 18300.0, u'Houston Rockets'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Palace of Auburn Hills', u'', u'', u'2 Championship Drive', u'', u'', u'', u'Auburn Hills', u'MI', u'48326', u'', u'Oakland', u'Detroit', 5.0, 42.69721, -83.24467, u'High', u'NGA', u'Shares arena with Shocks (WNBA).', u'Eastern', u'Central', 22076.0, u'Detroit Pistons'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'AT&T Center', u'', u'', u'1 AT&T Center Parkway', u'', u'', u'', u'San Antonio', u'TX', u'78219', u'', u'Bexar', u'San Antonio', 6.0, 29.42703, -98.43766, u'High', u'NGA', u'Shares arena with Silver Stars (WNBA), Rampage (AHL).', u'Western', u'Southwest', 18581.0, u'San Antonio Spurs'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Ford Center', u'', u'', u'100 West Reno Avenue', u'', u'', u'', u'Oklahoma City', u'OK', u'73102', u'73102-2473', u'Oklahoma', u'Oklahoma City', 6.0, 35.46347, -97.51483, u'High', u'NGA', u'', u'Western', u'Southwest', 18203.0, u'Oklahoma City Thunder'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20100215', u'', u'', u'Air Canada Centre', u'', u'', u'40 Bay Street', u'', u'', u'', u'Toronto', u'ON', u'M5J', u'2X2', u'', u'', 0.0, 43.64333, -79.37916, u'High', u'NGA', u'Shares arena with Maple Leafs (NHL) and Rock (NLL).', u'Eastern', u'Atlantic', 19800.0, u'Toronto Raptors']]
Traceback (most recent call last):
File "C:/Users/test.py", line 22, in <module>
arena_shapefile_tk = shapefile.Reader(arenafile_tk)
File "C:\Python27\lib\site-packages\shapefile.py", line 546, in __init__
raise ShapefileException("Shapefile Reader requires a shapefile or file-like object.")
shapefile.ShapefileException: Shapefile Reader requires a shapefile or file-like object.
Process finished with exit code 1
简答:尝试:
arenafile_tk = tkFileDialog.askopenfilename()
而不是:
arenafile_tk = tkFileDialog.askopenfile()
并相应地调整参数。
长答案:一个shapefile由多个文件组成,当你打开“.shp”文件名时,它寻找其他:
arena_shapefile = shapefile.Reader(r"data\Arenas_NBA\Arenas_NBA.shp")
如果您尝试传递由 askopenfile()
返回的打开文件句柄,它希望 您 提供有关其他文件的信息:
arena_shp_file = open(r"data\Arenas_NBA\Arenas_NBA.shp", "rb")
arena_dbf_file = open(r"data\Arenas_NBA\Arenas_NBA.dbf", "rb")
arena_shapefile = shapefile.Reader(shp=arena_shp_file, dbf=arena_dbf_file)
或类似的。
我正在尝试通过 Tkinter 和 tkFileDialog 加载和读取 shapefile。
以下代码显示了所做的工作。
首先我尝试直接读取 shapefile 并打印其内容 - 没有错误,然后使用 Tkinter 对话框读取相同的 shapefile 产生错误。
import shapefile
import Tkinter, tkFileDialog
# Read the Arena shapefile using the Reader class of the pyshp module
arenafile = r"data\Arenas_NBA\Arenas_NBA.shp"
arena_shapefile = shapefile.Reader(arenafile)
arena_shapes = arena_shapefile.shapes()
arena_records = arena_shapefile.records()
print arena_records
root = Tkinter.Tk()
root.withdraw()
arenafile_tk = tkFileDialog.askopenfile(parent=root,
mode='rb',
initialdir="data\Arenas_NBA",
title="Select Arena Shapefile",
filetypes=(("shapefiles", "*.shp"), ("all files", "*.*")))
if arenafile_tk != None:
arena_shapefile_tk = shapefile.Reader(arenafile_tk)
arena_shapes_tk = arena_shapefile_tk.shapes()
arena_records_tk = arena_shapefile_tk.records()
arenafile_tk.close()
print arena_records_tk
此代码产生以下输出:
[[u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20040211', u'20100215', u'', u'American Airlines Arena', u'', u'', u'601 Biscayne Boulevard', u'', u'', u'', u'Miami', u'FL', u'33132', u'', u'Dade', u'Miami-Fort Lauderdale', 4.0, 25.78117, -80.18758, u'High', u'NGA', u'', u'Eastern', u'Southeast', 19600.0, u'Miami Heat'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20040211', u'20100215', u'', u'ARCO Arena', u'', u'', u'1 Sports Parkway', u'', u'', u'', u'Sacramento', u'CA', u'95834', u'', u'Sacramento', u'Sacramento', 9.0, 38.64932, -121.51869, u'High', u'NGA', u'', u'Western', u'Pacific', 17317.0, u'Sacramento Kings'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20040211', u'20100215', u'', u'EnergySolutions Arena', u'', u'', u'301 West South Temple', u'', u'', u'', u'Salt Lake City', u'UT', u'84101', u'', u'Salt Lake', u'Salt Lake City-Ogden', 8.0, 40.76629, -111.90093, u'High', u'NGA', u'Shares arean with Thunder (AF1).', u'Western', u'Northwest', 19911.0, u'Utah Jazz'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20040226', u'20100215', u'', u'Fed Ex Forum', u'', u'', u'191 Beale Street', u'', u'', u'', u'Memphis', u'TN', u'38103', u'', u'Shelby', u'Memphis', 4.0, 35.1381, -90.0513, u'High', u'NGA', u'Shares arena with Memphis Tigers (NCAA).', u'Western', u'Southwest', 18119.0, u'Memphis Gizzlies'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Quicken Loans Arena', u'', u'', u'1 Center Court', u'', u'', u'', u'Cleveland', u'OH', u'44115', u'44115-4001', u'Cuyahoga', u'Cleveland-Akron', 5.0, 41.49648, -81.6887, u'High', u'NGA', u'Shares areana with Monsters (AHL) and Gladiators (AF1)', u'Eastern', u'Central', 20562.0, u'Cleveland Cavaliers'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Oracle Arena', u'', u'', u'7000 Coliseum Way', u'', u'', u'', u'Oakland', u'CA', u'94621', u'94621-1917', u'Alameda', u'San Francisco-Oakland', 9.0, 37.75131, -122.20217, u'High', u'NGA', u'', u'Western', u'Pacific', 19596.0, u'Golden State Warriors'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'New Orleans Arena', u'', u'', u'1601 Girod Street', u'', u'', u'', u'New Orleans', u'LA', u'70113', u'70113-3124', u'Orleans', u'New Orleans', 6.0, 29.94889, -90.08203, u'High', u'NGA', u'', u'Western', u'Southwest', 18500.0, u'New Orleans Hornets'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Rose Garden', u'', u'', u'1 Center Court', u'', u'', u'', u'Portland', u'OR', u'97227', u'', u'Multnomah', u'Portland', 10.0, 45.53165, -122.66692, u'High', u'NGA', u'Shares areana with Winterhawks (WHL).', u'Western', u'Northwest', 20630.0, u'Portland Trailblazers'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Amway Arena', u'', u'', u'600 West Amelia Street', u'', u'', u'', u'Orlando', u'FL', u'32801', u'', u'Orange', u'Orlando', 4.0, 28.54883, -81.38636, u'High', u'NGA', u'Shares arena with Titans (NLL) and Predators (AF1)', u'Eastern', u'Southeast', 17519.0, u'Orlando Magic'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Bradley Center', u'', u'', u'1001 North 4th Street', u'', u'', u'', u'Milwaukee', u'WI', u'53203', u'53203-1314', u'Milwaukee', u'Milwaukee', 5.0, 43.04346, -87.91736, u'High', u'NGA', u'Shares arena with Marquette Golden Eagles (NCAA)', u'Eastern', u'Central', 18717.0, u'Milwaukee Bucks'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'American Airlines Center', u'', u'', u'2500 Victory Avenue', u'', u'', u'', u'Dallas', u'TX', u'75219', u'75219-7601', u'Dallas', u'Dallas-Fort Worth', 6.0, 32.79062, -96.81043, u'High', u'NGA', u'Shares arena with Stars (NHL)..', u'Western', u'Southwest', 20337.0, u'Dallas Mavericks'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'TD Garden Center', u'', u'', u'100 Legends Way', u'', u'', u'', u'Boston', u'MA', u'02114', u'02114-4714', u'Suffolk', u'Boston', 1.0, 42.36653, -71.06206, u'High', u'NGA', u'Shares arena with Bruins (NHL) and Blazers (NLL).', u'Eastern', u'Atlantic', 18624.0, u'Boston Celtics'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Pepsi Center', u'', u'', u'1000 Chopper Circle', u'', u'', u'', u'Denver', u'CO', u'80204', u'', u'Denver', u'Denver', 8.0, 39.74916, -105.00677, u'High', u'NGA', u'Shares arena with Avalanche (NHL) and Mammoth (NLL).', u'Western', u'Northwest', 19155.0, u'Denver Nuggets'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Philips Arena', u'', u'', u'1 Philips Drive NW', u'', u'', u'', u'Atlanta', u'GA', u'30303', u'', u'Fulton', u'Atlanta', 4.0, 33.7573, -84.39631, u'High', u'NGA', u'Shares arena with Thashers (NHL) and Dream (WNBA).', u'Eastern', u'Southeast', 19445.0, u'Atlanta Hawks'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'United Center', u'', u'', u'1902 West Madison Street', u'', u'', u'', u'Chicago', u'IL', u'60612', u'60612-2459', u'Cook', u'Chicago', 5.0, 41.88059, -87.67418, u'High', u'NGA', u'Shares arena with Blackhawks (NHL).', u'Eastern', u'Central', 20917.0, u'Chicago Bulls'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Wachovia Center', u'', u'', u'3601 South Broad Street', u'', u'', u'', u'Philadelphia', u'PA', u'19148', u'', u'Philadelphia', u'Philadelphia', 3.0, 39.90097, -75.17218, u'High', u'NGA', u'Shares arena with Flyers (NHL) and Wings (NLL).', u'Eastern', u'Atlantic', 21600.0, u'Philadelphia 76ers'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'IZOD Center', u'', u'', u'50 Route 120', u'', u'', u'', u'East Rutherfor', u'NJ', u'07073', u'', u'Bergen', u'New York-Newark', 2.0, 40.81163, -74.06767, u'High', u'NGA', u'', u'Eastern', u'Atlantic', 20049.0, u'New Jersey Nets'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Madison Square Garden', u'', u'', u'4 Pennsylvania Plaza', u'', u'', u'', u'New York', u'NY', u'10001', u'', u'New York', u'New York-Newark', 2.0, 40.7506, -73.99378, u'High', u'NGA', u'Shares arena with Rangers (NHL), Saint John Red Storm (NCAA), and Liberty (WNBA).', u'Eastern', u'Atlantic', 19763.0, u'New York Knicks'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Verizon Center', u'', u'', u'601 F Street NW', u'', u'', u'', u'Washington', u'DC', u'20004', u'', u'District of Columbi', u'Washington', 3.0, 38.89838, -77.02098, u'High', u'NGA', u'Shares arena with Capitals (NHL), Mystics (WNBA) and Georgetown Hoyas (NCAA)', u'Eastern', u'Southeast', 20173.0, u'Washington Wizards'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Staples Center', u'', u'', u'1111 South Figueroa Street', u'', u'', u'', u'Los Angeles', u'CA', u'90015', u'', u'Los Angeles', u'Los Angeles', 9.0, 34.04321, -118.26716, u'High', u'NGA', u'Shares arena with Kings (NHL), Sparks (WNBA), and D-Fenders(D-League).', u'Western', u'Pacific', 18997.0, u'Los Angeles Lakers, Clipppers'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Target Center', u'', u'', u'600 First Avenue North', u'', u'', u'', u'Minneapolis', u'MN', u'55403', u'', u'Hennepin', u'Minneapolis-Saint Paul', 5.0, 44.97944, -93.27609, u'High', u'NGA', u'Shares arena with Lynx (WNBA).', u'Western', u'Northwest', 20500.0, u'Minnesota Timberwolves'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'US Airways Center', u'', u'', u'201 East Jefferson', u'', u'', u'', u'Phoenix', u'AZ', u'85004', u'85004-2412', u'Maricopa', u'Phoenix', 8.0, 33.446, -112.07125, u'High', u'NGA', u'Shares arena with Mercury (WNBA), Rattlers (AF1)', u'Western', u'Pacific', 18422.0, u'Phoenix Suns'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Time Warner Cable Arena', u'', u'', u'333 East Trade Street', u'', u'', u'', u'Charlotte', u'NC', u'28202', u'', u'Mecklenburg', u'Charlotte', 4.0, 35.22373, -80.83999, u'High', u'NGA', u'Shares arena with Checkers (AHL),', u'Eastern', u'Southeast', 19026.0, u'Charlotte Bobcats'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Conseco Fieldhouse', u'', u'', u'125 South Pennsylvania Street', u'', u'', u'', u'Indianapolis', u'IN', u'46204', u'', u'Marion', u'Indianapolis', 5.0, 39.76401, -86.15557, u'High', u'NGA', u'Shares arena with Fever (WNBA).', u'Eastern', u'Central', 18345.0, u'Indiana Pacers'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Toyota Center (Houston)', u'', u'', u'1510 Polk Street', u'', u'', u'', u'Houston', u'TX', u'77002', u'77002-7130', u'Harris', u'Houston', 6.0, 29.75078, -95.36218, u'High', u'NGA', u'Shares arena with Comets (WNBA).', u'Western', u'Southwest', 18300.0, u'Houston Rockets'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Palace of Auburn Hills', u'', u'', u'2 Championship Drive', u'', u'', u'', u'Auburn Hills', u'MI', u'48326', u'', u'Oakland', u'Detroit', 5.0, 42.69721, -83.24467, u'High', u'NGA', u'Shares arena with Shocks (WNBA).', u'Eastern', u'Central', 22076.0, u'Detroit Pistons'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'AT&T Center', u'', u'', u'1 AT&T Center Parkway', u'', u'', u'', u'San Antonio', u'TX', u'78219', u'', u'Bexar', u'San Antonio', 6.0, 29.42703, -98.43766, u'High', u'NGA', u'Shares arena with Silver Stars (WNBA), Rampage (AHL).', u'Western', u'Southwest', 18581.0, u'San Antonio Spurs'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20060411', u'20100215', u'', u'Ford Center', u'', u'', u'100 West Reno Avenue', u'', u'', u'', u'Oklahoma City', u'OK', u'73102', u'73102-2473', u'Oklahoma', u'Oklahoma City', 6.0, 35.46347, -97.51483, u'High', u'NGA', u'', u'Western', u'Southwest', 18203.0, u'Oklahoma City Thunder'], [u'High Value/Symbolic', u'Sports Arena', u'NBA', u'20100215', u'', u'', u'Air Canada Centre', u'', u'', u'40 Bay Street', u'', u'', u'', u'Toronto', u'ON', u'M5J', u'2X2', u'', u'', 0.0, 43.64333, -79.37916, u'High', u'NGA', u'Shares arena with Maple Leafs (NHL) and Rock (NLL).', u'Eastern', u'Atlantic', 19800.0, u'Toronto Raptors']]
Traceback (most recent call last):
File "C:/Users/test.py", line 22, in <module>
arena_shapefile_tk = shapefile.Reader(arenafile_tk)
File "C:\Python27\lib\site-packages\shapefile.py", line 546, in __init__
raise ShapefileException("Shapefile Reader requires a shapefile or file-like object.")
shapefile.ShapefileException: Shapefile Reader requires a shapefile or file-like object.
Process finished with exit code 1
简答:尝试:
arenafile_tk = tkFileDialog.askopenfilename()
而不是:
arenafile_tk = tkFileDialog.askopenfile()
并相应地调整参数。
长答案:一个shapefile由多个文件组成,当你打开“.shp”文件名时,它寻找其他:
arena_shapefile = shapefile.Reader(r"data\Arenas_NBA\Arenas_NBA.shp")
如果您尝试传递由 askopenfile()
返回的打开文件句柄,它希望 您 提供有关其他文件的信息:
arena_shp_file = open(r"data\Arenas_NBA\Arenas_NBA.shp", "rb")
arena_dbf_file = open(r"data\Arenas_NBA\Arenas_NBA.dbf", "rb")
arena_shapefile = shapefile.Reader(shp=arena_shp_file, dbf=arena_dbf_file)
或类似的。