Python CSV头未正确写入

2024-05-14 09:13:57 发布

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

我想写两个标题,但我收到我的实际两个标题和3个空白字段。这可能是什么原因?这是我的脚本和输出。该脚本解析jsonweb服务并将总线的lat/lng发送到Esri文件地理数据库。你知道吗

enter image description here

enter image description here

import requests
import json
import urllib2
import csv
import arcpy





if arcpy.Exists("C:\MYLATesting.gdb\API_Table"):
    arcpy.Delete_management("C:\MYLATesting.gdb\API_Table")

url = "http://api.metro.net/agencies/lametro/vehicles/"
parsed_json = json.load(urllib2.urlopen("http://api.metro.net/agencies/lametro/vehicles/"))
details = {'items': 'longitude'}
headers = {'Content-type': 'application/x-www-form-urlencoded', 'Accept': '/'}
response = requests.get(url, data=json.dumps(details), headers=headers)


# tell computer where to put CSV
outfile_path='C:\Users\Administrator\Desktop\API_Testing.csv'

# open it up, the w means we will write to it
writer = csv.writer(open(outfile_path, 'wb'))

#create a list with headings for our columns
headers = ['latitude','longitude']

for items in parsed_json['items']:
    row = []
    row.append(str(items['latitude']).encode('utf-8'))
    row.append(str(items['longitude']).encode('utf-8'))
    writer.writerow(row)

i = 1
i = i +1

f = open(outfile_path, 'wb')
writer = csv.writer(f)
#write the row of headings to our CSV file
writer.writerow(headers)

f.close()

#Temporary Delete Function to Overwrite Table

arcpy.TableToTable_conversion(outfile_path, "C:\MYLATesting.gdb", "API_Table")

print response.text

Tags: csvtopathimportapijsontableitems
1条回答
网友
1楼 · 发布于 2024-05-14 09:13:57

额外的空白列可能意味着两件事之一: 1您的脚本在每行末尾输出额外的逗号(,),您的电子表格程序将其解释为额外的列或列 2TableToTable\u转换函数正在做一些看起来像是数据库模式的奇怪的事情。你知道吗

您是否通过数据库套件查看简历?在记事本上贴一张屏幕截图

相关问题 更多 >

    热门问题