簡體   English   中英

Python CSV標頭未正確編寫

[英]Python CSV headers are not writing correctly

我想寫兩個標題,但是正在接收我實際的兩個標題和3個空白字段。 這可能是什么原因? 這是我的腳本和輸出。 該腳本解析一個JSON Web服務,並將總線的經/緯發送到Esri文件地理數據庫。

在此輸入圖像描述

在此輸入圖像描述

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

多余的空白列可能意味着兩件事之一:1.您正在腳本中在每行的末尾輸出額外的逗號(,),您的電子表格程序會將其解釋為額外的列;或者2. TableToTable_conversion函數的作用很奇怪似乎是數據庫架構的東西。

您是否正在通過數據庫套件查看簡歷? 在記事本中發布屏幕截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM