如何在python中确定一周的第一天

How to determine the first day of week in python(如何在python中确定一周的第一天)
本文介绍了如何在python中确定一周的第一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

根据语言环境,我需要找到一周的第一天(周日/周一)在 JAVA 中我会这样做:

Based on the locale I need to find what is the first day of the week (Sunday/Monday) In JAVA I would do:

Calendar FR_cal = Calendar.getInstance(Locale.FRANCE);
Calendar CA_cal = Calendar.getInstance(Locale.CANADA);

DateFormatSymbols dfs = new DateFormatSymbols();
String weekdays[] = dfs.getWeekdays();

System.out.println(weekdays[JO_cal.getFirstDayOfWeek()]);
System.out.println(weekdays[FR_cal.getFirstDayOfWeek()]);

我如何在 python 中做到这一点?

How can I do it in python ?

推荐答案

我只能想办法用 Babel 库.它可以通过 easy_install 获得.

I could only figure out how to do this with the Babel library. It's available through easy_install.

>>> import babel
>>> locale = babel.Locale('en', 'US')
>>> locale.first_week_day
6
>>> locale.days['format']['wide'][locale.first_week_day]
u'Sunday'

<小时>

事实证明,以下内容不起作用,因为 Nate 好心指出.如果有人知道原因,请发布并回答以说明如何正确处理.这应该可以通过标准库来实现.


turns out that the following doesn't work as Nate was kind enough to point out. If anybody knows why, please post and answer showing how to do it right. This should be doable with the standard library.

如果你只想要一天的数字,那么你可以使用calendar.LocalTextCalendar.

If you just want the number of the day, then you can use calendar.LocalTextCalendar.

>>> import calendar
>>> c = calendar.LocaleTextCalendar(locale='de_DE') # locale=('en_US', 'UTF8') fails too.
>>> c.firstweekday
0

还有 iterweekdays 方法.

>>> list(c.iterweekdays())
[0, 1, 2, 3, 4, 5, 6]

这篇关于如何在python中确定一周的第一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Seasonal Decomposition of Time Series by Loess with Python(Loess 用 Python 对时间序列进行季节性分解)
Resample a time series with the index of another time series(使用另一个时间序列的索引重新采样一个时间序列)
How can I simply calculate the rolling/moving variance of a time series in python?(如何在 python 中简单地计算时间序列的滚动/移动方差?)
How to use Dynamic Time warping with kNN in python(如何在python中使用动态时间扭曲和kNN)
Keras LSTM: a time-series multi-step multi-features forecasting - poor results(Keras LSTM:时间序列多步多特征预测 - 结果不佳)
Python pandas time series interpolation and regularization(Python pandas 时间序列插值和正则化)