我有一个csv文件,我想打印到屏幕上,然后每次更改文件时都重新读取。我有python工作格式化&打印出文件,但我一直在检查时间戳,所以它会重新读取&更改后打印。它只是给了我一个我无法理解的语法错误。我知道路径是正确的,因为它是用来打开csv文件的。
这就是错误:
File "C:Python27last50.py", line 7
def __init__(self): self._cached_stamp = 0 self.filename = 'C:UsersAdministratorAppDataLocalWsjt-xwsjtx.log'
^
Syntax error: invalid syntax
以下是完整的脚本:
#!/usr/bin/python
import csv
import sys
import os
class Monkey(object):
def __init__(self): self._cached_stamp = 0 self.filename = 'C:UsersAdministratorAppDataLocalWsjt-xwsjtx.log'
def ook(self): stamp = os.stat(self.filename).st_mtime
if stamp != self._cached_stamp: self._cached_stamp = stamp
# File has changed, so do something...
with open('C:UsersAdministratorAppDataLocalWsjt-xwsjtx.log') as csvfile:
fieldnames = ('date1', 'time1', 'date2','time2','callsign',)
reader = csv.DictReader(csvfile, fieldnames=fieldnames)
count = 0
headers = dict( (n,n) for n in fieldnames )
for row in reader:
count += 1
print'{:<9} {:12} {:12}'.format(row['callsign'],row['date1'],row['time1'])
print "Found " + str(count) + " rows"
它确实是缩进Martineau-现在修复了,谢谢。