#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    9
    Rep Power
    0

    Text file import validator


    Hi guys,

    I'm trying to import an ASCII file into my app but the file contains non ascii characters (,,π,) and its borking the whole process.
    So what I've done is write a script that checks the file for those non ascii characters and prints out the offending line.
    It also checks that a date value in my text file is valid.

    What I'd like it return to the user depending on the circumstances is:
    -Invalid date on line 1
    -Invalid characters on the following lines: 1, 8, 10, 38, 78
    -File is ready to be imported


    Here is my code:
    Code:
    # -*- coding: utf-8 -*-
    import datetime
    
    
    def is_ascii(s):
        return all(ord(c) < 128 for c in s)
    
    
    def meta_data(first_line):
        return {'log_date_day': first_line[7:9].strip(),
                'log_date_month': first_line[9:11].strip(),
                'log_date_year': first_line[11:13].strip()}
    
    
    def correct_date(year_month_day_format):
        minyear = 2013
        maxyear = datetime.date.today().year
        dateparts = date_event.split("-")
        try:
            if len(dateparts) != 3:
                raise ValueError("Invalid date format")
            if int(dateparts[0]) > maxyear or int(dateparts[0]) < minyear:
                raise ValueError("Year out of range")
            datetime.date(int(dateparts[0]), int(dateparts[1]), int(dateparts[2]))
            return True
        except:
            return False
    
    
    with open('MYFILE', 'r') as the_file:
        data_list = the_file.readlines()
    
    
    header_data = meta_data(data_list[0])
    date_event = "20" + header_data['log_date_year'] + "-" + header_data['log_date_month'] + "-" + header_data['log_date_day']
    
    
    for index, line in enumerate(data_list):
        line_of_fault = index
        line_of_fault += 1
        if correct_date(date_event) is False:
            print "Error: The date in line 1 is incorrect"
            break
        elif is_ascii(line) is False:
            print "%s: " % line_of_fault, data_list[index]
    I think I'm 90% if someone could help me with the rest that would be great
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    157
    Rep Power
    2
    What is it doing wrong?

IMN logo majestic logo threadwatch logo seochat tools logo