如何在文件 JSON 中查找数字并使用 python-docx 放置换行符

How to find numbers in file JSON and put newline using python-docx

我有一个包含字符串数据的 JSON 文件。我想找到“1”。取决于 '。'并在“.”之后添加一个换行符 输入是这样的:

My mom goes to market today, she buys 1. Apples at the fruit store. 2. Beef at the meat shop. 3. Knife at the material shop. 4. She back home at 8 a.m. 5. But she forgets to buy Vegetables. 6. And she backs to market again. 7. And finally, she buys everything that she needs.

我想要这样的输出:

My mom goes to market today, she buys 
1. Apples at the fruit store. 
2. Beef at the meat shop. 
3. Knife at the material shop. 
4. She back home at 8 a.m. 
5. But she forgets to buy Vegetables. 
6. And she backs to market again. 
7. And finally, she buys everything that she needs.

并且输出变成.docx 文件。 有人能帮我吗?谢谢。

如果你的字符串在变量中,我会遍历它,因为你可以遍历字符。

您想知道什么时候数字后跟一个点

my_string = 'My mom goes to market today, she buys 1. Apples at the fruit store. 2. Beef at the meat shop. 3. Knife at the material shop. 4. She back home at 8 a.m. 5. But she forgets to buy Vegetables. 6. And she backs to market again. 7. And finally, she buys everything that she needs.'


# Helper function to check if the character can be casted to an integer
def RepresentsInt(s):
    try: 
        int(s)
        return True
    except ValueError:
        return False

new_string = ''

# We enumerate so when we find a number we can access 
# the following char and check it's a dot
for i, char in enumerate(my_string):
    if RepresentsInt(char):
         if my_string[i + 1] == '.':
             new_string += ('\n' + char)
         else:
             new_string += char
    else:
        new_string += char

然后将my_string的值替换为new_string

new_string = 'My mom goes to market today, she buys \n1. Apples at the fruit store. \n2. Beef at the meat shop. \n3. Knife at the material shop. \n4. She back home at 8 a.m. \n5. But she forgets to buy Vegetables. \n6. And she backs to market again. \n7. And finally, she buys everything that she needs.'