1
我试图转换具有以下列的CSV文件:csv文件转换为另一种CSV选择特定的列蟒蛇
ID,Name,Postcode,State,Suburb,Lat,Lon 1,Hurstville Store,1493,NSW,Hurstville,-33.975869,151.088939
我想和新的CSV只有名称,纬度,经度列,但即时得到这个错误: 头= csvReader.next() AttributeError的: '_csv.reader' 对象有没有属性 '下一个'
这里是我到目前为止的代码:
import csv
# Set up input and output variables for the script
storeLoc = open("store_locations.csv", "r")
# Set up CSV reader and process the header
csvReader = csv.reader(storeLoc)
header = csvReader.next()
nameIndex = header.index("Name")
latIndex = header.index("Lat")
lonIndex = header.index("Lon")
# Make an empty list
coordList = []
# Loop through the lines in the file and get each coordinate
for row in csvReader:
name = row[nameIndex]
lat = row[latIndex]
lon = row[lonIndex]
coordList.append([name,lat,lon])
# Print the coordinate list
print(coordList)
coordList.append([name,lat,lon])
stores = open('store_coords.csv','w', newline='')
感谢您的任何反馈
谢谢你们的回应,我发现在另一个线程 – Blake
一个超级简单的解决方案感谢您的解决方案寿它没有解决我的代码:) – Blake
@Blake:没问题。我怀疑你的“超级简单解决方案”比这个更好或更容易。 – mhawke