我尝试用Python翻译日期。如果字符串中包含“ January”,则将其替换为“ janvier”等。
我遍历json数据库。 但是,如果同一字符串中有两个月(“ 9月”和“十月”),则仅翻译第一个。为什么?
datestring = datestring.replace(" - ", "-")
# Trad french/english
if "January" or "january" in datestring:
x["auction_date"] = datestring.replace("January", "janvier")
if "February" in datestring:
x["auction_date"] = datestring.replace("February", "février")
if "March" in datestring:
x["auction_date"] = datestring.replace("March", "mars")
if "April" in datestring:
x["auction_date"] = datestring.replace("April", "avril")
...
答案 0 :(得分:2)
您需要将替换分配回日期字符串,因为字符串在Python中是不可变的:
datestring = datestring.replace(" - ", "-")
# Trad french/english
if "January" or "january" in datestring:
datestring = datestring.replace("January", "janvier")
if "February" in datestring:
datestring = datestring.replace("February", "février")
if "March" in datestring:
datestring = datestring.replace("March", "mars")
if "April" in datestring:
datestring = datestring.replace("April", "avril")
# now make the assignment to your data frame
x["auction_date"] = datestring
...
答案 1 :(得分:1)
在此示例中,您不需要有条件。 Protected Async Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Await (addMeeting("MPP Test By Milind with API", 1, False, True, False, "", ""))
End Sub
本身就是支票。我建议以下。
datestring.replace("January", "janvier")
这可能不是最有效的,但可能更易于管理。
祝你好运。
P.S:想指出一个常见的麻烦
您有:
>>> datestring = "JanUarY"
>>> def english_to_french(month):
... month = month.lower()
... month = month.replace("january", "janvier")
... month = month.replace("february", "février")
... return month
...
>>> x = english_to_french(datestring) >>> print(x)
janvier
此检查中的 # Trad french/english
if "January" or "january" in datestring:
x["auction_date"] = datestring.replace("January", "janvier")
语句不会评估您的想法。 if
之后的每个条件都将独立处理。这是一个例子。
and/or
相反
>>> if "January":
... print(True) ...
True
>>> mystr = "Bobby" >>> if "January" or "january" in mystr: ... print("Bobby will always be in January") ...
Bobby will always be in January
还是最好的!
>>> if "January" in mystr or "january" in mystr:
... print("Bobby will never be in January")
...
>>>