【问题标题】:Python String split() Method + Export ["each pieces split "] in different .txt filesPython String split() 方法 + 导出 ["eachpieces split"] 在不同的 .txt 文件中
【发布时间】:2021-11-22 07:02:06
【问题描述】:

Python String split() 方法 + 导出 ["eachpieces split"] 在不同的 .txt 和 .jpg 文件中,为每种文件类型指定路径文件夹

您好,我学习 Python,我必须对文本进行不同的操作:

  1. 拆分文本
  2. 打印拆分文本

  1. 将 ["each pieces split"] 包含在 print(x) 中导出到不同的 .txt 和 .jpg 文件,并为每种文件类型指定路径文件夹

步骤 1 和 2 已完成:

    txt = "This is the first sentence. Then, the second                               
    sentence. And at the end, the last sentence."
    
    x = txt.split (".")

    print(x)

    ['This is the first sentence', ' Then the second sentence',   
    ' And at the end, the last sentence', '']

我想做什么:

  1. 将 ["each pieces split"] 包含在 print(x) 中导出到不同的 .txt 和 .jpg 文件,并为每种文件类型指定路径文件夹

有人知道我们如何制作这样的东西吗?


感谢您的帮助

【问题讨论】:

    标签: python text split export txt


    【解决方案1】:

    方法之一:

    txt = """This is the first sentence. Then, the second                               
    sentence. And at the end, the last sentence."""
    
    sentences = txt.split (".")
    path_to_files = 'C:\\Users\\username\\Desktop\\Split txt export'
    for index in range(len(sentences)):
        with open(f'{path_to_files}\\file_{index}', 'w') as f:
            f.write(sentences[index])
    

    这是你想要的吗?在上述方法中,将根据形成的句子数量创建文件。所以如果有 10 个句子,一个文件夹中会创建 10 个文件。

    【讨论】:

    • 这正是我在阅读您的代码提案下方的描述时搜索的内容,但我不知道如何执行它?我必须替换的是'/path/to/folder',但是在你的代码目的中还有其他信息可以替换吗?我有一个 SyntaxError: (unicode error) 'unicodeescape'。非常感谢@Bhagyesh
    • txt = "这是第一句。然后是第二句。最后是最后一句。"句子 = txt.split (".") path_to_files = 'C:\Users\username\Desktop\Split txt export' for index in range(len(sentences)): with open(f'{path_to_files}/file_{index} ', 'w') as f: f.write(sentences[index]) 这是对的吗?
    • 如果它帮助并解决了您的问题,如果您接受答案,那就太好了。谢谢!
    • 失败的原因是你的路径中的'\',它应该使用黑斜线作为'\\'进行转义,所以现在你的路径变为:path_to_files = 'C:\\Users\\username\\Desktop\\Split txt export',已经用这个更新了代码,请现在检查你不会得到你提到的错误。
    • 是的,也可以这样做。谢谢!但是由于OP刚开始学习Python,就想到了一种不使用内置的简单方法..
    【解决方案2】:

    希望这会有所帮助。您可以使用os.mkdir('dirname') 创建目录。将此与@metapod 的答案结合使用,以便从列表中获取目录名称。

        txt = "This is the first sentence. Then, the second sentence. And at the end, the last sentence."
        x = txt.split(".")
    
        for i, line in enumerate(x):
            os.mkdir(f'line_{i}')
            os.mkdir(f'line_{i}/text')
            os.mkdir(f'line_{i}/image')
    
            f = open(f"line_{i}/text/line_{i}.txt", 'w')
            f.write(line)
            f.close()
    
            f = open(f"line_{i}/image/line_{i}.jpg", 'w')
            f.write(line)
            f.close()
    

    【讨论】:

    • 谢谢您,我现在还没有执行的级别,但我会尽快检查!
    • 我忘了提到你必须在所有代码之前import os
    【解决方案3】:

    如果你知道文件名:

    filenames: ["path/to/file1", "path/to/file2", "path/to/file3"]
    txt = ["Set", "of", "strings"]
    
    for filename, subtxt in zip(filenames, txt):
        with open(filename, 'w') as file:
            file.write(subtxt)
    

    file1 将包含 txt[0]、file2 txt[1]、...

    这适用于 txt 文件,不知道如何使用 jpg 文件(甚至不知道你想做什么)。

    【讨论】:

    • 我不知道文件名,因为目标是将它们命名并导出为(文件 1、文件 2、文件 3 等...),以便在我需要的“_ _ _”之间分割的每个句子探索 Bhagyesh Dudhediya 解决方案,但我有一个“SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape”用他的解决方案,我不知道为什么(谢谢你回答但我不能用手写文件名会太长)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2012-12-09
    相关资源
    最近更新 更多