Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用分隔符时忽略字符之间_Python_Json - Fatal编程技术网

Python 使用分隔符时忽略字符之间

Python 使用分隔符时忽略字符之间,python,json,Python,Json,我正在使用python,我希望能够打印出一个文件并分离这些值。为了添加一些上下文,我将从Json文件中取出选定的值,更改这些值,然后将它们放回Json文件中。我可以做每一步 但我有一行文字有问题 "Hello, the date goes from {{exercise.start_date|pretty_date}} to {{exercise.end_date|pretty_date}}." 基本上,它是通过一种变通方法完成的,但我的问题是,有没有一种方法可以忽略不同的部分,例如,{{{}

我正在使用python,我希望能够打印出一个文件并分离这些值。为了添加一些上下文,我将从Json文件中取出选定的值,更改这些值,然后将它们放回Json文件中。我可以做每一步

但我有一行文字有问题

"Hello, the date goes from {{exercise.start_date|pretty_date}} to {{exercise.end_date|pretty_date}}."
基本上,它是通过一种变通方法完成的,但我的问题是,有没有一种方法可以忽略不同的部分,例如,{{{}和}}之间的所有内容

那么我想要得到的输出是

"HELLO, THE DATE GOES FROM {{exercise.start_date|pretty_date}} TO {{exercise.end_date|pretty_date}}."

我想到的一种方法是:

st = "Hello, the date goes from {{exercise.start_date|pretty_date}} to {{exercise.end_date|pretty_date}}."

split_list = st.split()  # split the string into a list
for i, sentence in enumerate(split_list):
    if not sentence.startswith('{{'):
        split_list[i] = sentence.upper()  # make the word uppercase if it's not between '{{ }}'
print(' '.join(split_list))
这将输出所需的结果:

您也可以按照建议在一行中实现这一点:

' '.join([word.upper() if not word.startswith('{{') else word for word in test.split()])

只要您不想在一行中使用另一个大写的
{{..}
,这将起作用:
'.join([word.upper()如果不是word.startswith(“{”)else在test.split()中逐字逐句)
是的,没错。我也在我的答案中添加了你的一行。我希望你不介意:)不,我正在研究列表理解方式,但这和你的逻辑是一样的,如果我有另一行{{…}有可能解决这个问题吗?仔细阅读我的上一句话。特别是最后一部分。我在谈论一些特殊情况,你可以使用{{word}}。在这种情况下,你必须使用正则表达式。
' '.join([word.upper() if not word.startswith('{{') else word for word in test.split()])