python类strptime()的实例源码

pyparsing.py 文件源码 项目:python- 作者: secondtonone1 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def convertToDate(fmt="%Y-%m-%d"):
        """
        Helper to create a parse action for converting parsed date string to Python datetime.date

        Params -
         - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%d"})

        Example::
            date_expr = pyparsing_common.iso8601_date.copy()
            date_expr.setParseAction(pyparsing_common.convertToDate())
            print(date_expr.parseString("1999-12-31"))
        prints::
            [datetime.date(1999, 12, 31)]
        """
        def cvt_fn(s,l,t):
            try:
                return datetime.strptime(t[0], fmt).date()
            except ValueError as ve:
                raise ParseException(s, l, str(ve))
        return cvt_fn
pyparsing.py 文件源码 项目:python- 作者: secondtonone1 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def convertToDatetime(fmt="%Y-%m-%dT%H:%M:%S.%f"):
        """
        Helper to create a parse action for converting parsed datetime string to Python datetime.datetime

        Params -
         - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%dT%H:%M:%S.%f"})

        Example::
            dt_expr = pyparsing_common.iso8601_datetime.copy()
            dt_expr.setParseAction(pyparsing_common.convertToDatetime())
            print(dt_expr.parseString("1999-12-31T23:59:59.999"))
        prints::
            [datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)]
        """
        def cvt_fn(s,l,t):
            try:
                return datetime.strptime(t[0], fmt)
            except ValueError as ve:
                raise ParseException(s, l, str(ve))
        return cvt_fn
pyparsing.py 文件源码 项目:python- 作者: secondtonone1 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def convertToDate(fmt="%Y-%m-%d"):
        """
        Helper to create a parse action for converting parsed date string to Python datetime.date

        Params -
         - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%d"})

        Example::
            date_expr = pyparsing_common.iso8601_date.copy()
            date_expr.setParseAction(pyparsing_common.convertToDate())
            print(date_expr.parseString("1999-12-31"))
        prints::
            [datetime.date(1999, 12, 31)]
        """
        def cvt_fn(s,l,t):
            try:
                return datetime.strptime(t[0], fmt).date()
            except ValueError as ve:
                raise ParseException(s, l, str(ve))
        return cvt_fn
pyparsing.py 文件源码 项目:python- 作者: secondtonone1 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def convertToDatetime(fmt="%Y-%m-%dT%H:%M:%S.%f"):
        """
        Helper to create a parse action for converting parsed datetime string to Python datetime.datetime

        Params -
         - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%dT%H:%M:%S.%f"})

        Example::
            dt_expr = pyparsing_common.iso8601_datetime.copy()
            dt_expr.setParseAction(pyparsing_common.convertToDatetime())
            print(dt_expr.parseString("1999-12-31T23:59:59.999"))
        prints::
            [datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)]
        """
        def cvt_fn(s,l,t):
            try:
                return datetime.strptime(t[0], fmt)
            except ValueError as ve:
                raise ParseException(s, l, str(ve))
        return cvt_fn
pyparsing.py 文件源码 项目:my-first-blog 作者: AnkurBegining 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def convertToDate(fmt="%Y-%m-%d"):
        """
        Helper to create a parse action for converting parsed date string to Python datetime.date

        Params -
         - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%d"})

        Example::
            date_expr = pyparsing_common.iso8601_date.copy()
            date_expr.setParseAction(pyparsing_common.convertToDate())
            print(date_expr.parseString("1999-12-31"))
        prints::
            [datetime.date(1999, 12, 31)]
        """
        def cvt_fn(s,l,t):
            try:
                return datetime.strptime(t[0], fmt).date()
            except ValueError as ve:
                raise ParseException(s, l, str(ve))
        return cvt_fn
pyparsing.py 文件源码 项目:my-first-blog 作者: AnkurBegining 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def convertToDatetime(fmt="%Y-%m-%dT%H:%M:%S.%f"):
        """
        Helper to create a parse action for converting parsed datetime string to Python datetime.datetime

        Params -
         - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%dT%H:%M:%S.%f"})

        Example::
            dt_expr = pyparsing_common.iso8601_datetime.copy()
            dt_expr.setParseAction(pyparsing_common.convertToDatetime())
            print(dt_expr.parseString("1999-12-31T23:59:59.999"))
        prints::
            [datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)]
        """
        def cvt_fn(s,l,t):
            try:
                return datetime.strptime(t[0], fmt)
            except ValueError as ve:
                raise ParseException(s, l, str(ve))
        return cvt_fn
pyparsing.py 文件源码 项目:my-first-blog 作者: AnkurBegining 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def convertToDate(fmt="%Y-%m-%d"):
        """
        Helper to create a parse action for converting parsed date string to Python datetime.date

        Params -
         - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%d"})

        Example::
            date_expr = pyparsing_common.iso8601_date.copy()
            date_expr.setParseAction(pyparsing_common.convertToDate())
            print(date_expr.parseString("1999-12-31"))
        prints::
            [datetime.date(1999, 12, 31)]
        """
        def cvt_fn(s,l,t):
            try:
                return datetime.strptime(t[0], fmt).date()
            except ValueError as ve:
                raise ParseException(s, l, str(ve))
        return cvt_fn
pyparsing.py 文件源码 项目:my-first-blog 作者: AnkurBegining 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def convertToDatetime(fmt="%Y-%m-%dT%H:%M:%S.%f"):
        """
        Helper to create a parse action for converting parsed datetime string to Python datetime.datetime

        Params -
         - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%dT%H:%M:%S.%f"})

        Example::
            dt_expr = pyparsing_common.iso8601_datetime.copy()
            dt_expr.setParseAction(pyparsing_common.convertToDatetime())
            print(dt_expr.parseString("1999-12-31T23:59:59.999"))
        prints::
            [datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)]
        """
        def cvt_fn(s,l,t):
            try:
                return datetime.strptime(t[0], fmt)
            except ValueError as ve:
                raise ParseException(s, l, str(ve))
        return cvt_fn
ygg.py 文件源码 项目:potatoygg 作者: Ripolin 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def getMoreInfo(self, nzb):
        """
        Get details about a torrent.

        .. seealso:: MovieSearcher.correctRelease
        """
        data = self.getHTMLData(nzb['detail_url'])
        soup = BeautifulSoup(data, 'html.parser')
        description = soup.find(id='description')
        if description:
            nzb['description'] = description.prettify()
        line = soup.find(text='Date de publication').parent.parent
        pub = line.find_all('td')[1]
        added = datetime.strptime(pub.getText().split('(')[0].strip(),
                                  '%d/%m/%Y %H:%M')
        nzb['age'] = (datetime.now() - added).days
        self.log.debug(nzb['age'])
crawler.py 文件源码 项目:MercrediFiction 作者: Meewan 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def save_account(instance, content):
    username = content['username']
    if not USERNAME_REGEX.match(username):
        username = '[invalid_username]'
    domain = instance.domain
    acct = "@" + username + "@" + domain
    if Account.query.filter_by(username=acct).count() != 0:
        return Account.query.filter_by(username=acct).first()
    else:
        creation_date = datetime.strptime(content['created_at'],
                                          "%Y-%m-%dT%H:%M:%S.%fZ")
        account = Account(mastodon_id=content['id'],
                          username=acct,
                          display_name=to_text(content['display_name']),
                          creation_date=creation_date,
                          note=to_text(content['note']),
                          url=validate_url(content['url']),
                          avatar=validate_url(content['avatar']),
                          instance=instance,
                          blacklisted=False)
        save(account)
        return account
event.py 文件源码 项目:mk42 作者: Peer-Lab 项目源码 文件源码 阅读 39 收藏 0 点赞 0 评论 0
def check_event_dates(self, request):
        """
        Check if new event end is later then start.

        This validation is implemented here 'cause model validation can't work with multiple fields.

        :param requeset: django request instance.
        :type request: django.http.request.HttpRequest.
        :return: is event dates validated?
        :rtype: bool.
        """

        start = datetime.strptime(request.data.get("start"), DATETIME_FORMAT)
        end = datetime.strptime(request.data.get("end"), DATETIME_FORMAT)

        return start < end
pyparsing.py 文件源码 项目:pip-update-requirements 作者: alanhamlett 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def convertToDate(fmt="%Y-%m-%d"):
        """
        Helper to create a parse action for converting parsed date string to Python datetime.date

        Params -
         - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%d"})

        Example::
            date_expr = pyparsing_common.iso8601_date.copy()
            date_expr.setParseAction(pyparsing_common.convertToDate())
            print(date_expr.parseString("1999-12-31"))
        prints::
            [datetime.date(1999, 12, 31)]
        """
        def cvt_fn(s,l,t):
            try:
                return datetime.strptime(t[0], fmt).date()
            except ValueError as ve:
                raise ParseException(s, l, str(ve))
        return cvt_fn
pyparsing.py 文件源码 项目:pip-update-requirements 作者: alanhamlett 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def convertToDatetime(fmt="%Y-%m-%dT%H:%M:%S.%f"):
        """
        Helper to create a parse action for converting parsed datetime string to Python datetime.datetime

        Params -
         - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%dT%H:%M:%S.%f"})

        Example::
            dt_expr = pyparsing_common.iso8601_datetime.copy()
            dt_expr.setParseAction(pyparsing_common.convertToDatetime())
            print(dt_expr.parseString("1999-12-31T23:59:59.999"))
        prints::
            [datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)]
        """
        def cvt_fn(s,l,t):
            try:
                return datetime.strptime(t[0], fmt)
            except ValueError as ve:
                raise ParseException(s, l, str(ve))
        return cvt_fn
message_segments.py 文件源码 项目:gloss 作者: openhealthcare 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def __init__(self, segment):
        self.lab_number = segment[3][0]
        self.profile_code = segment[4][0][0][0]
        self.profile_description = segment[4][0][1][0]
        self.request_datetime = datetime.strptime(
            segment[6][0][:12], DATETIME_FORMAT
        )
        self.observation_datetime = datetime.strptime(
            segment[7][0][:12], DATETIME_FORMAT
        )

        self.last_edited = datetime.strptime(
            segment[22][0], DATETIME_FORMAT
        )

        self.result_status = TEST_STATUS_MAPPING[segment[25][0]]
message_segments.py 文件源码 项目:gloss 作者: openhealthcare 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def __init__(self, segments):
        self.allergy_type = segments[2][0][0][0]
        self.allergy_type_description = segments[2][0][1][0]
        self.certainty_id = segments[2][0][3][0]
        self.certainty_description = segments[2][0][4][0]
        self.allergy_reference_name = segments[3][0][0][0]
        self.allergy_description = segments[3][0][1][0]

        self.allergen_reference_system = None
        if len(segments[3][0]) > 2:
            self.allergen_reference_system = segments[3][0][2][0]

        self.allergen_reference = None
        if len(segments[3][0]) > 3:
            self.allergen_reference = segments[3][0][3][0]

        self.status_id = segments[4][0][0][0]
        self.status_description = segments[4][0][1][0]
        self.diagnosis_datetime = datetime.strptime(
            segments[4][0][4][0], DATETIME_FORMAT
        )
        self.allergy_start_datetime = datetime.strptime(
            segments[6][0], DATETIME_FORMAT
        )
__init__.py 文件源码 项目:onefl-deduper 作者: ufbmi 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def format_date(val, fmt='%m-%d-%Y'):
    """
    Transform the input string to a datetime object

    :param val: the input string for date
    :param fmt: the input format for the date
    """
    date_obj = None

    try:
        date_obj = datetime.strptime(val, fmt)
    except Exception as exc:
        log.warning("Problem formatting date: {} - {} due: {}"
                    .format(val, fmt, exc))

    return date_obj
util.py 文件源码 项目:FRG-Crowdsourcing 作者: 97amarnathk 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _last_activity_points(project):
    default = datetime(1970, 1, 1, 0, 0).strftime('%Y-%m-%dT%H:%M:%S')
    updated_datetime = (project.get('updated') or default)
    last_activity_datetime = (project.get('last_activity_raw') or default)
    updated_datetime = updated_datetime.split('.')[0]
    last_activity_datetime = last_activity_datetime.split('.')[0]
    updated = datetime.strptime(updated_datetime, '%Y-%m-%dT%H:%M:%S')
    last_activity = datetime.strptime(last_activity_datetime, '%Y-%m-%dT%H:%M:%S')
    most_recent = max(updated, last_activity)

    days_since_modified = (datetime.utcnow() - most_recent).days

    if days_since_modified < 1:
        return 50
    if days_since_modified < 2:
        return 20
    if days_since_modified < 3:
        return 10
    if days_since_modified < 4:
        return 5
    return 0
rewritedates.py 文件源码 项目:transfer 作者: viur-framework 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def handleEntryResponse(self, modul, entryIn ):
        assert self.cfg is not None
        appID = self.sink.getAppId()
        if not modul in self.cfg.keys():
            print("RewriteDates cannot process a modul it doesn't know anything about: %s" % modul)
            return( modul, entryIn )
        modulCfg = self.cfg[ modul ]
        for boneName, boneInfo in modulCfg:
            if "type" in boneInfo.keys() and boneInfo["type"]=="date":
                if boneName in entryIn.keys() and entryIn[boneName]:
                    data = entryIn[boneName]
                    if isinstance( data, unicode ) and data!="None":
                        try:
                            data = datetime.strptime(str( data ), "%d.%m.%Y %H:%M:%S")
                        except:
                            pass
                        entryIn[boneName] = data
        return( modul, entryIn )
imdb_crawl.py 文件源码 项目:holcrawl 作者: shaypal5 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def _get_reviews_props(movie_code):
    cur_reviews_url = _REVIEWS_URL.format(code=movie_code)
    reviews_page = bs(urllib.request.urlopen(cur_reviews_url), "html.parser")
    reviews = reviews_page.find_all("td", {"class": "comment-summary"})
    user_reviews = []
    for review in reviews:
        try:
            rating = int(re.findall(_USER_REVIEW_RATING_REGEX, str(review))[0])
            date_str = re.findall(
                r"on (\d{1,2} [a-zA-Z]+ \d{4})", str(review))[0]
            date = datetime.strptime(date_str, "%d %B %Y").date()
            contents = review.find_all(
                'a', href=re.compile(r'reviews.+?'))[0].contents[0]
            user = review.find_all(
                'a', href=re.compile(r'/user/.+?'))[1].contents[0]
            user_reviews.append({
                'score': rating, 'review_date': date,
                'contents': contents, 'user': user
            })
        except Exception:  # pylint: disable=W0703
            pass
    return {'imdb_user_reviews': user_reviews}


# ==== crawling a movie profile ====
metacritic_crawl.py 文件源码 项目:holcrawl 作者: shaypal5 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def _get_user_review_props(review):
    review_props = {}
    date_str = review.find_all("span", {"class": "date"})[0].contents[0]
    date_str = _parse_date_str(date_str)
    review_props['review_date'] = datetime.strptime(
        date_str, "%B %d, %Y").date()
    review_props['score'] = int(review.find_all(
        "div", {"class": "metascore_w"})[0].contents[0])
    try:
        review_props['text'] = review.find_all(
            'span', {'class': 'blurb blurb_expanded'})[0].contents[0].strip()
    except IndexError:
        review_props['text'] = review.find_all(
            'div', {'class': 'review_body'})[0].contents[1].contents[0].strip()
    review_props['user'] = review.find_all(
        'span', {'class': 'author'})[0].contents[0].contents[0]
    review_props['total_reactions'] = int(review.find_all(
        'span', {'class': 'total_count'})[0].contents[0])
    review_props['pos_reactions'] = int(review.find_all(
        'span', {'class': 'yes_count'})[0].contents[0])
    review_props['neg_reactions'] = review_props[
        'total_reactions'] - review_props['pos_reactions']
    return review_props
ingest_jobscripts.py 文件源码 项目:supremm 作者: ubccr 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def pathfilter(path, mindate):
    """ return whether path should not be processed based on mindate
        return value of False indicates no filtering
        return value of true indicates the path should be filtered
    """

    if mindate == None:
        return False

    subdir = os.path.basename(path)
    try:
        if datetime.strptime(subdir, "%Y%m%d") < mindate:
            logging.debug("Skip(1) subdir %s", subdir)
            return True
    except ValueError:
        logging.debug("Skip(2) subdir %s", subdir)
        return True

    return False
pyparsing.py 文件源码 项目:swjtu-pyscraper 作者: Desgard 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def convertToDate(fmt="%Y-%m-%d"):
        """
        Helper to create a parse action for converting parsed date string to Python datetime.date

        Params -
         - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%d"})

        Example::
            date_expr = pyparsing_common.iso8601_date.copy()
            date_expr.setParseAction(pyparsing_common.convertToDate())
            print(date_expr.parseString("1999-12-31"))
        prints::
            [datetime.date(1999, 12, 31)]
        """
        def cvt_fn(s,l,t):
            try:
                return datetime.strptime(t[0], fmt).date()
            except ValueError as ve:
                raise ParseException(s, l, str(ve))
        return cvt_fn
pyparsing.py 文件源码 项目:swjtu-pyscraper 作者: Desgard 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def convertToDatetime(fmt="%Y-%m-%dT%H:%M:%S.%f"):
        """
        Helper to create a parse action for converting parsed datetime string to Python datetime.datetime

        Params -
         - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%dT%H:%M:%S.%f"})

        Example::
            dt_expr = pyparsing_common.iso8601_datetime.copy()
            dt_expr.setParseAction(pyparsing_common.convertToDatetime())
            print(dt_expr.parseString("1999-12-31T23:59:59.999"))
        prints::
            [datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)]
        """
        def cvt_fn(s,l,t):
            try:
                return datetime.strptime(t[0], fmt)
            except ValueError as ve:
                raise ParseException(s, l, str(ve))
        return cvt_fn
wayback.py 文件源码 项目:waybackscraper 作者: abrenaut 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def list_archive_timestamps(url, min_date, max_date, user_agent):
    """
    List the available archive between min_date and max_date for the given URL
    """
    logger.info('Listing the archives for the url {url}'.format(url=url))

    # Construct the URL used to download the memento list
    parameters = {'url': url,
                  'output': 'json',
                  'from': min_date.strftime(WEB_ARCHIVE_TIMESTAMP_FORMAT),
                  'to': max_date.strftime(WEB_ARCHIVE_TIMESTAMP_FORMAT)}
    cdx_url = WEB_ARCHIVE_CDX_TEMPLATE.format(params=urlencode(parameters))

    req = Request(cdx_url, None, {'User-Agent': user_agent})
    with urlopen(req) as cdx:
        memento_json = cdx.read().decode("utf-8")

        timestamps = []
        # Ignore the first line which contains column names
        for url_key, timestamp, original, mime_type, status_code, digest, length in json.loads(memento_json)[1:]:
            # Ignore archives with a status code != OK
            if status_code == '200':
                timestamps.append(datetime.strptime(timestamp, WEB_ARCHIVE_TIMESTAMP_FORMAT))

    return timestamps
cli.py 文件源码 项目:waybackscraper 作者: abrenaut 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def main():
    args = parse_args()

    logging.basicConfig(level=(logging.WARN if args.quiet else logging.INFO))

    # Don't allow more than 10 concurrent requests to the wayback machine
    concurrency = min(args.concurrency, 10)

    # Scrape results are stored in a temporary folder if no folder specified
    target_folder = args.target_folder if args.target_folder else tempfile.gettempdir()
    logger.info('Writing scrape results in the folder {target_folder}'.format(target_folder=target_folder))

    # Parse the period entered by the user (throws an exception if the dates are not correctly formatted)
    from_date = datetime.strptime(args.from_date, CLI_DATE_FORMAT)
    to_date = datetime.strptime(args.to_date, CLI_DATE_FORMAT)

    # The scraper downloads the elements matching the given xpath expression in the target folder
    scraper = Scraper(target_folder, args.xpath)

    # Launch the scraping using the scraper previously instantiated
    scrape_archives(args.website_url, scraper.scrape, from_date, to_date, args.user_agent, timedelta(days=args.delta),
                    concurrency)
dataset.py 文件源码 项目:Optimizer-cotw 作者: alkaya 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def load_from_file(filename):
        '''
        Load and return data from file
        :param filename: path of the database.csv file
        :return: (date, latitude, longitude, magnitude) (np.array)
        '''
        date, latitude, longitude, magnitude = [], [], [], []

        with open(filename, "r") as f:
            f.readline()  # Skip first line

            for line in f:
                elements = line.split(',')
                try:
                    date.append(datetime.strptime("{} {}".format(elements[0], elements[1]), "%m/%d/%Y %H:%M:%S"))
                    latitude.append(float(elements[2]))
                    longitude.append(float(elements[3]))
                    magnitude.append(elements[8])
                except ValueError:
                    pass

        return np.array(date), np.float32(latitude), np.float32(longitude), np.float32(magnitude)
resp.py 文件源码 项目:pyrsss 作者: butala 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def parse_block_header(fid):
    """
    Parse the block header found in *fid*. Return a mapping containing
    the header information.
    """
    block_header = {}
    for line in fid:
        if line.startswith('#'):
            break
        toks = line.split()
        key = '_'.join(toks[1:-1])[:-1]
        value = toks[-1]
        if key.endswith('date'):
            value = datetime.strptime(value, '%Y,%j,%H:%M:%S')
        block_header[key] = value
    return block_header
iaga2002.py 文件源码 项目:pyrsss 作者: butala 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def iaga2df(iaga2002_fname, D_to_radians=True):
    """
    Parser the magnetometer data record stored in the IAGA-2002 format
    file *iaga2002_fname*. If *D_to_radians*, declination data (D) are
    converted from degrees to radians. Return the tuple with the
    :class:`DataFrame` containing the data and header information
    """
    with open(iaga2002_fname) as fid:
        # parse header
        header, cols = parse_header(fid)
        keys = ['B_' + x for x in cols]
        # parse data
        index = []
        data_map = defaultdict(list)
        for line in fid:
            toks = line.split()
            dt = datetime.strptime(toks[0] + ' ' + toks[1], '%Y-%m-%d %H:%M:%S.%f')
            index.append(dt)
            data = map(convert_float, toks[3:])
            for key_i, data_i in zip(keys, data):
                if key_i == 'B_D' and D_to_radians:
                    data_i = math.radians(data_i)
                data_map[key_i].append(data_i)
    df = PD.DataFrame(index=index, data=data_map)
    return df, header
sm2hdf.py 文件源码 项目:pyrsss 作者: butala 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def read_sm_csv(csv_fname):
    """
    Parse the SuperMAG CSV format data record *csv_fname*. For each
    station, store the information in pandas
    :class:`DataFrame`. Return a mapping between the station
    identifier and data frame.
    """
    df = PD.read_csv(csv_fname,
                     header=0,
                     parse_dates=[0],
                     date_parser=lambda x: datetime.strptime(x, '%Y-%m-%d %H:%M:%S'),
                     index_col=0)
    df_map = {name: group for name, group in df.groupby('IAGA')}
    for df in df_map.itervalues():
        del df['IAGA']
        df.rename(columns={'N': 'B_N',
                           'E': 'B_E',
                           'Z': 'B_Z'},
                  inplace=True)
    return df_map
p1c1.py 文件源码 项目:pyrsss 作者: butala 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def __init__(self, p1c1_fname=P1C1_FNAME):
        """
        Parse *p1c1_fname* and store DCB (in [TECU]) in the mapping
        :class:`datetime` -> ['svn', 'prn'] -> integer ID.
        """
        super(P1C1Table, self).__init__()
        if p1c1_fname == P1C1_FNAME and not os.path.isfile(p1c1_fname):
            update_p1c1()
        with open(p1c1_fname) as fid:
            for line in fid:
                if line.startswith('#'):
                    continue
                cols = line.split()
                date = datetime.strptime(cols[0], '%Y-%m-%d')
                prn = int(cols[1])
                svn = int(cols[2])
                CA_P_m = float(cols[3])
                self.setdefault(date, {}).setdefault('prn', {})[prn] = CA_P_m
                self[date].setdefault('svn', {})[svn] = CA_P_m


问题


面经


文章

微信
公众号

扫码关注公众号