【问题标题】:How to split a text file based on the number of characters in Python如何根据Python中的字符数拆分文本文件
【发布时间】:2021-07-28 11:04:34
【问题描述】:

我有一个大文件,想根据字符数将文本文件拆分为多个文件。每个文件的字符数必须小于 100000。

例如,输入文件test.txt 会变成```test1.txt, test2.txt, test3.txt... test1932.txt''' 等等。

我有以下逻辑。

with open("test.txt") as inFile:
    sentence = inFile.read().split()

character_count = 0
output_sentence = ""
fileCount = 0
outputName = "test" + fileCount + ".txt"

for word in sentence:
    word = word.replace(",", "")
    character_count = character_count + len(word)
    if character_count < 100000:
        output_sentence = output_sentence + word + " "
    else:
        fileCount = fileCount + 1
        break

with open(outputName, "w") as outputFile:
    outputFile.write(output_sentence)

但是,我有点纠结于如何循环它以使其不断生成新文件。我该如何做到这一点?

【问题讨论】:

  • 把它写在你的else子句里,有什么问题?

标签: python python-3.x


【解决方案1】:

您似乎错过了在循环中创建文件以及更新变量sentence 以获取当前文件的内容。这个怎么样:

for word in sentence:
    word = word.replace(",", "")
    character_count = character_count + len(word)
    if character_count < 100000:
        output_sentence = output_sentence + word + " "
    else:
        character_count = 0
        outputName = "test" + fileCount + ".txt"

       with open(outputName, "w") as outputFile:
           outputFile.write(output_sentence)

       fileCount = fileCount + 1
       output_sentence = ''
        

【讨论】:

  • 这不会从 0 开始计数吧?因为我想确保在达到 100000 后继续创建新文件
【解决方案2】:
f = open('YourFile.txt', 'r')
c,d = 0,0
s = f.read()
f.close()
w = ""
for ch in s:
    c = c + 1
    w = w + ch
    if c == 100000:
        d = d + 1
        g = open('test'+str(d)+'.txt', 'w')
        g.write(w)
        g.close()
        w = ""
        c = 0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    相关资源
    最近更新 更多