Priyan RockZ
Priyan RockZ

Reputation: 1615

Python Import text file

I'm used below code for import text document.

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]

here is my text document text (Finger print machine output text file)

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

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

here shows my error

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?

EDIT I change methods as below then file read and gives error when second line read

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

New error

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

Upvotes: 0

Views: 590

Answers (1)

Priyan RockZ
Priyan RockZ

Reputation: 1615

    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]

Its now sorted.i check every times when new lines comes then its ok

:-)

Upvotes: 1

Related Questions