社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python3通过分隔符将大文件拆分为小文件(不是大小、行)

workerbee • 6 年前 • 2264 次点击  

新来的。最终的任务是学习如何获取两个大的yaml文件并将它们分成几百个小文件。我还没有弄清楚如何使用ID作为文件名,所以一次只能做一件事。

-
    ID: 627
    more_post_meta_data_and_content
-
    ID: 628

这是我的代码,它不起作用。到目前为止,我不明白为什么:

with open('test-file.yml', 'r') as myfile:
    start = 0
    cntr = 1
    holding = ''
    for i in myfile.read().split('\n'):
        if (i == '-\n'):
            if start==1:
                with open(str(cntr) + '.md','w') as opfile:
                    opfile.write(op)
                    opfile.close()
                    holding=''
                    cntr += 1
            else:
                start=1
        else:
            if holding =='':
                holding = i
            else:
                holding = holding + '\n' + i
    myfile.close()

欢迎所有提示、建议和指针。谢谢。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/55035
文章 [ 3 ]  |  最新文章 6 年前
darksky
Reply   •   1 楼
darksky    7 年前

当你在一个 with context with 当你离开这个街区时,会自动为你关门。所以你不需要 file.close() 任何地方。

readlines 它输出一个生成器,该生成器一次从打开的文件中读取一行。这比 read() 后面跟着 split() . 想想看。你在内存中加载一个大文件,然后要求CPU通过 \n 性格。效率不高。

opfile.write(op) . 这是哪里 op 定义?你不想把内容写进 holding 你定义的?

with open('test.data', 'r') as myfile:
    counter = 1
    content = ""
    start = True

    for line in myfile.readlines():
        if line == "-\n" and not start:
            with open(str(counter) + '.md', 'w') as opfile:
                opfile.write(content)

            content = ""
            counter += 1
        else:
            if not start:
                content += line

        start = False

    # write the last file if test-file.yml doesn't end with a dash
    if content != "":
        with open(str(counter) + '.md', 'w') as opfile:
            opfile.write(content)
Zephyx
Reply   •   2 楼
Zephyx    7 年前

作为一个新手,乍一看你试图写一个未声明的变量op到你的输出。您几乎是当场,只需遍历您的操作文件并编写内容:

    with open('test-file.yml', 'r') as myfile:
        start = 0
        cntr = 1
        holding = ''
        for i in myfile.read().split('\n'):
            if (i == '-\n'):
                if start==1:
                    with open(str(cntr) + '.md','w') as opfile:
                        for line in opfile:
                            op = line
                            opfile.write(op)
                            opfile.close()
                            holding=''
                            cntr += 1
                else:
                    start=1
            else:
                if holding =='':
                    holding = i
                else:
                    holding = holding + '\n' + i
        myfile.close()

希望这有帮助!

tripleee
Reply   •   3 楼
tripleee    7 年前

如果输入文件很大,将整个文件读入内存,然后分割内存区域是非常低效的。请改为:

with open('test-file.yml', 'r') as myfile:
    opfile = None
    cntr = 1
    for line in myfile:
        if line == '-\n':
            if opfile is not None:
                opfile.close()
            opfile = open('{0}.md'.format(cntr),'w')
            cntr += 1
        opfile.write(line)
    opfile.close()

close 你打开的东西 with 上下文管理器;上下文管理器的目的就是为您解决这个问题。