scheduler生成空文件

时间:2017-04-20 13:18:27

标签: python-2.7 pythonanywhere

我正在使用pythonanywhere进行简单的计划任务。 我想每天从链接下载一次数据并保存csv文件。后来,一旦我有一个不错的时间序列,我会弄清楚我实际上是如何管理数据的。这不是太多的数据,所以不需要像数据库那样花哨的东西。

我的脚本从google工作表链接获取数据,添加日志列和时间列,然后在文件名中写入带日期的csv。

当我在pythonanywhere中手动运行它时,它完全符合我的要求,但调度程序只是创建空的csv文件,尽管名称正确。

任何想法是什么?我不明白日志文件。当手动运行时,错误应该会发生吗?

脚本:

import pandas as pd
import time
import datetime

def write_today(df):
    date = time.strftime("%Y-%m-%d")
    df.to_csv('Properties_'+date+'.csv')


url = 'https://docs.google.com/spreadsheets/d/19h2GmLN-2CLgk79gVxcazxtKqS6rwW36YA-qvuzEpG4/export?format=xlsx'
df = pd.read_excel(url, header=1).rename(columns={'Unnamed: 1':'code'})
source = pd.read_excel(url).columns[0]
df['source'] = source
df['time'] = datetime.datetime.now()
write_today(df)

调度程序设置如下:

日志文件:

Traceback (most recent call last):
  File "/home/abmoore/load_data.py", line 24, in <module>
    write_today(df)
  File "/home/abmoore/load_data.py", line 16, in write_today
    df.to_csv('Properties_'+date+'.csv')
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1344, in to_csv
    formatter.save()
  File "/usr/local/lib/python2.7/dist-packages/pandas/formats/format.py", line 1551, in save
    self._save()
  File "/usr/local/lib/python2.7/dist-packages/pandas/formats/format.py", line 1638, in _save
    self._save_header()
  File "/usr/local/lib/python2.7/dist-packages/pandas/formats/format.py", line 1634, in _save_header
    writer.writerow(encoded_labels)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 0: ordinal not in range(128)

1 个答案:

答案 0 :(得分:1)

您的问题是UnicodeDecodeError - 您的电子表格中有一些非ascii数据,而pandas to_csv函数默认为ascii编码。请尝试指定utf8:

def write_today(df):
    filename = 'Properties_{date}.csv'.format(date=time.strftime("%Y-%m-%d"))
    df.to_csv(filename, encoding='utf8')

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_csv.html