Python脚本错误:无法读取csv文件

时间:2018-03-20 09:50:38

标签: python python-3.x python-2.7 subprocess

我试图逐行处理tab csv文件下面。它引发错误。无法追踪我错在哪里。

这是文件:

/tmp/fa.csv

1       Close
6       Close
72      Close
99      Close
8       Close
4       Close
3       Close
103     Close
106     Close
107     Close
105     Close
220     Open

9.py

import csv
with open('/tmp/fa.csv') as f:
rown = csv.reader(f,delimiter='\t')
for row in rown:
print row[1]

输出:

[root@localhost ~]# python 9.py
  File "9.py", line 3
    rown = csv.reader(f,delimiter='\t')
       ^
IndentationError: expected an indented block

2 个答案:

答案 0 :(得分:3)

IndentationError错误。推送with语句

中的内容

<强>实施例

import csv
with open('/tmp/fa.csv') as f:
    rown = csv.reader(f,delimiter='\t')
    for row in rown:
        print row[1]

答案 1 :(得分:3)

您获得的错误是indentation错误,而不是您的代码中的逻辑。

这是完整的工作代码: -

import csv
with open('/tmp/fa.csv') as f:
    rown = csv.reader(f,delimiter='\t')
    for row in rown:
        print row