硬编码csv文件的第一列,然后使用Python导入google分析
有没有办法硬编码csv文件的第一列,然后从第二列开始从google analytics导入我的数据 我的问题是,当我硬编码第一列然后上传数据时,我的Google analytics数据打印在csv文件的第一列而不是第二列硬编码csv文件的第一列,然后使用Python导入google分析,python,csv,google-analytics,Python,Csv,Google Analytics,有没有办法硬编码csv文件的第一列,然后从第二列开始从google analytics导入我的数据 我的问题是,当我硬编码第一列然后上传数据时,我的Google analytics数据打印在csv文件的第一列而不是第二列 def print_csv_file(results): with open("Google Analytics.csv", "wt") as csvfile: if results.get('rows', []): # do headers into
def print_csv_file(results):
with open("Google Analytics.csv", "wt") as csvfile:
if results.get('rows', []):
# do headers into output_row
csvfile.write('Site \n')
for i in range(5):
csvfile.write('siteName')
csvfile.write('\n')
output_row = ""
for header in results.get('columnHeaders'):
output_row += '"' + header.get('name') + '"' + ','
csvfile.write(output_row + '\n')
# do cells into output_row
for row in results.get('rows'):
output_row = ""
counter = 1
for cell in row:
if cell.isdigit():
output_row += cell +','
else:
try:
float(cell)
output_row+= cell +','
except ValueError:
output_row += '"' + cell + '"'+','
counter +=1
csvfile.write(output_row + '\n')
else:
csvfile.write('No Results Found')
def print_top_pages(service, start_date, end_date):
# Print out informat in that's queried.
query = service.data().ga().get(
ids = GA_SITE_CODE,
start_date = start_date,
end_date = end_date,
dimensions = 'ga:year,ga:pagePath',
metrics = 'ga:pageviews,ga:uniquePageviews,ga:avgTimeOnPage,ga:entrances,ga:bounceRate,ga:exitRate',
sort = '-ga:pageviews',
start_index = 1,
max_results = 10000).execute()
print_csv_file(query)
在处理CSV数据时,应使用
CSV
库,该库可简化读写。因为你的问题不是很清楚,我只能猜测你想做什么。为此,请看一下您的代码重写:
import csv
def print_csv_file(results):
with open('Google Analytics.csv', 'w') as output_file:
writer = csv.writer(output_file)
if 'rows' not in results:
writer.writerow(['No Results Found'])
return
header = ['Site'] + [cell['name'] for cell in results['columnHeaders']]
writer.writerow(header)
for row in results['rows']:
row.insert(0, 'SiteName')
writer.writerow(row)
在处理CSV数据时,应使用
CSV
库,该库可简化读写。因为你的问题不是很清楚,我只能猜测你想做什么。为此,请看一下您的代码重写:
import csv
def print_csv_file(results):
with open('Google Analytics.csv', 'w') as output_file:
writer = csv.writer(output_file)
if 'rows' not in results:
writer.writerow(['No Results Found'])
return
header = ['Site'] + [cell['name'] for cell in results['columnHeaders']]
writer.writerow(header)
for row in results['rows']:
row.insert(0, 'SiteName')
writer.writerow(row)
请显示样本输入和预期输出。请显示样本输入和预期输出。