社区所有版块导航
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 年前 • 2268 次点击  

新来的。最终的任务是学习如何获取两个大的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 年前