【问题标题】:Sublime3: read dataframe from file, which name given by command line argumentSublime3:从文件中读取数据帧,名称由命令行参数给出
【发布时间】:2015-11-25 00:11:33
【问题描述】:

我一直在使用 ipython notebook,但现在我应该传递给脚本 .py。在编写普通脚本时,在 ipython 中看起来很容易的事情变得更加困难(如果我误解了某些内容,请纠正我)。 因此,例如,我需要从文件中读取数据帧(然后对这个数据帧执行一些操作),我想在命令行中将其名称作为参数给出:

python myfile.py filename.csv

为此,我编写了以下脚本:

import pandas as pd
import numpy as np
import datetime
import time
import sys

def read_file(filesrc):
    df = pd.read_csv(filesrc, 
                     sep= '\t', 
                     names=['id', 'date', 'name', 'age'], 
                     parse_dates=['date'], 
                     infer_datetime_format=True)
    print "Reading file..."
    df1 = (df.drop(['id'], 1)
             .sort_index(by=['date'], ascending=True)
             .reset_index()
             .drop('index', 1))
    return df1.to_csv('just_df.csv')

def main():
    filesrc = sys.argv[1]
    read_file(filesrc)

if __name__ == '__main__':
    main()

但我总是出错:

  df1 = df.drop(['id'], 1).sort_index(by=['date'], ascending=True).reset_index().drop('index', 1)     
                                                                                            ^
    IndentationError: unindent does not match any outer indentation level

我注意制表符和空格缩进,并且在 Sublime3 中工作,在这种情况下我没有任何关于缺少或额外缩进的警告,所以我很惊讶地看到这个错误,我猜可能有问题脚本的其他部分,可能来自 sys.argv[1] 或其他内容,因此我们将非常感谢您的帮助和建议。

【问题讨论】:

  • IndentationError 只能是缩进错误!仔细检查一下。在 Sublime 上,尝试查看 -> 缩进 -> 将缩进转换为制表符...并查看该行是否超出缩进级别。
  • @alec_djinn 你是对的!您可以将其发布为答案,因为它解决了我的问题!

标签: python pandas dataframe command-line-arguments indentation


【解决方案1】:

IndentationError 只能是缩进错误!仔细检查一下。在 Sublime 上,尝试查看 -> 缩进 -> 将缩进转换为制表符...并查看该行是否超出缩进级别。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-06
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 2017-07-24
    • 2011-08-30
    • 2017-02-21
    • 2013-04-02
    相关资源
    最近更新 更多