0

我在下面的代码中使用了导入文本文档。

class text_based_attendance(osv.osv):

    def text_files_upload(self, cr, uid, ids, context=None):
        attendance_v={}
        attendance_obj = self.pool.get('text.based.attendance')
        print('Text Files going to upload')
        with open("/home/priyan/Desktop/Store/HR_Module/attendance/savefile.txt") as f:
            c = csv.reader(f, delimiter=':', skipinitialspace=True)
            for line in c:
                name=line[0]

这是我的文本文档文本(指纹机输出文本文件)

09D043543011301292:0834*0073*:3:5:G

09D043543011301292:1744*0073*:3:5:G

这里显示了我的错误

2013-08-16 11:16:00,792 5291 ERROR bell_hr_beta openerp.osv.osv: Uncaught exception
Traceback (most recent call last):
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 131, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 197, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 185, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/addons/hr_attendance/hr_attendance.py", line 227, in text_files_upload
    for line in c:
Error: new-line character seen in unquoted field - do you need to open the file in universal-newline mode?

编辑 我改变方法如下然后文件读取并在第二行读取时给出错误

with open("/home/priyan/Desktop/Store/HR_Module/attendance/savefile.txt",'rU') as f:

新错误

2013-08-16 11:22:05,972 5382 ERROR bell_hr_beta openerp.osv.osv: Uncaught exception
Traceback (most recent call last):
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 131, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 197, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/osv/osv.py", line 185, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "/home/priyan/hr_openerp/openerp-7.0/openerp/addons/hr_attendance/hr_attendance.py", line 228, in text_files_upload
    name=line[0]
IndexError: list index out of range
4

1 回答 1

1
    with open("/home/priyan/Desktop/Store/HR_Module/attendance/savefile.txt",'rU') as f:
        c = csv.reader(f, delimiter=':', skipinitialspace=True)
        for line in c:
            if line:
                name=line[0]

它现在已排序。我每次都有新行时检查一次,然后就可以了

:-)

于 2013-08-16T06:02:30.210 回答