Skip to main content
 首页 » 编程设计

python之如何更新文本文件中给定章节中子章节的数量

2025年02月15日7powertoolsteam

我有一个文本文件,其中包含一本书的目录。
我必须创建一个可以被 ghostscript 读取的 index.txt 文件。

文本文件可用HERE ,看起来像:

Chapter 1 Introduction 1 
Chapter 2 Fundamental Observations 7 
2.1 Dark night sky   7 
2.2 Isotropy and homogeneity  11 
2.3 Redshift proportional to distance  15 
2.4 Types of particles  22 
2.5 Cosmic microwave background  28 
Chapter 3 Newton Versus Einstein 32 
3.1 Equivalence principle  33 
3.2 Describing curvature  39 
3.3 Robertson-Walker metric  44 
3.4 Proper distance 47    

这必须更改为:

[/Count -0 /Page 7 /Title (Chapter: 1 Introduction ) /OUT pdfmark 
[/Count -5 /Page 13 /Title (Chapter: 2 Fundamental Observations ) /OUT pdfmark 
[/Count 0 /Page 13 /Title (Chapter: 2.1 Dark night sky   ) /OUT pdfmark 
[/Count 0 /Page 17 /Title (Chapter: 2.2 Isotropy and homogeneity  ) /OUT pdfmark 
[/Count 0 /Page 21 /Title (Chapter: 2.3 Redshift proportional to distance  ) /OUT pdfmark 
[/Count 0 /Page 28 /Title (Chapter: 2.4 Types of particles  ) /OUT pdfmark 
[/Count 0 /Page 34 /Title (Chapter: 2.5 Cosmic microwave background  ) /OUT pdfmark 
[/Count -4 /Page 38 /Title (Chapter: 3 Newton Versus Einstein ) /OUT pdfmark 
[/Count 0 /Page 39 /Title (Chapter: 3.1 Equivalence principle  ) /OUT pdfmark 
[/Count 0 /Page 45 /Title (Chapter: 3.2 Describing curvature  ) /OUT pdfmark 
[/Count 0 /Page 50 /Title (Chapter: 3.3 Robertson-Walker metric  ) /OUT pdfmark 
[/Count 0 /Page 53 /Title (Chapter: 3.4 Proper distance   ) /OUT pdfmark 

在上面,注意:

Count = number of sub chapter in the given chapter   
Page = given page in table of content + 6  

我们如何做到这一点?

到目前为止,我已经尝试过了。

def get_Count_Page_and_Title(bookmark, offset=6): 
    """Get chapters and page numbers.""" 
    with open(bookmark, 'r') as fi, open('temp_index.txt', 'w') as fo: 
        for line in fi: 
            line = r'[/Count -0 /Page 0 /Title (Chapter: 1 Introduction ) /OUT pdfmark' 
            print(line, file = fo) 

一些相关链接是:
python reading text file
Read .txt file line by line in Python

请您参考如下方法:

这是一种解析文件的方法。此代码使用简单的字符串匹配来区分章节和子章节行。然后它将每个子章节与其封闭的章节组合起来。最后,它将迭代此数据以生成所需的输出。

代码:

def print_count_page_and_title(data, page_offset=0): 
    """Get chapters and page numbers.""" 
    chapters = [] 
    chapter = None 
    for line in data: 
        if line.startswith('Chapter'): 
            if chapter is not None: 
                chapters.append(chapter) 
            chapter = (line.strip().rsplit(' ', 1), []) 
        else: 
            chapter[1].append(line.strip().rsplit(' ', 1)) 
 
    if chapter is not None: 
        chapters.append(chapter) 
 
    def page_num(page): 
        return int(page) + page_offset 
 
    fmt_chapter = '[/Count -%d /Page %d /Title (%s) /OUT pdfmark' 
    fmt_sub_chapter = '[/Count 0 /Page %d /Title (%s) /OUT pdfmark' 
 
    for chapter in chapters: 
        print(fmt_chapter % ( 
            len(chapter[1]), page_num(chapter[0][1]), chapter[0][0])) 
        for sub_chapter in chapter[1]: 
            print(fmt_sub_chapter % ( 
                page_num(sub_chapter[1]), sub_chapter[0])) 
 
print_count_page_and_title(test_data, page_offset=6) 

测试数据:

from io import StringIO 
 
test_data = StringIO(u'\n'.join([x.strip() for x in """ 
    Chapter 1 Introduction 1 
    Chapter 2 Fundamental Observations 7 
    2.1 Dark night sky   7 
    2.2 Isotropy and homogeneity  11 
    2.3 Redshift proportional to distance  15 
    2.4 Types of particles  22 
    2.5 Cosmic microwave background  28 
    Chapter 3 Newton Versus Einstein 32 
    3.1 Equivalence principle  33 
    3.2 Describing curvature  39 
    3.3 Robertson-Walker metric  44 
    3.4 Proper distance 47    
""".split('\n')[1:-1]])) 

结果:

[/Count -0 /Page 7 /Title (Chapter 1 Introduction) /OUT pdfmark 
[/Count -5 /Page 13 /Title (Chapter 2 Fundamental Observations) /OUT pdfmark 
[/Count 0 /Page 13 /Title (2.1 Dark night sky  ) /OUT pdfmark 
[/Count 0 /Page 17 /Title (2.2 Isotropy and homogeneity ) /OUT pdfmark 
[/Count 0 /Page 21 /Title (2.3 Redshift proportional to distance ) /OUT pdfmark 
[/Count 0 /Page 28 /Title (2.4 Types of particles ) /OUT pdfmark 
[/Count 0 /Page 34 /Title (2.5 Cosmic microwave background ) /OUT pdfmark 
[/Count -4 /Page 38 /Title (Chapter 3 Newton Versus Einstein) /OUT pdfmark 
[/Count 0 /Page 39 /Title (3.1 Equivalence principle ) /OUT pdfmark 
[/Count 0 /Page 45 /Title (3.2 Describing curvature ) /OUT pdfmark 
[/Count 0 /Page 50 /Title (3.3 Robertson-Walker metric ) /OUT pdfmark 
[/Count 0 /Page 53 /Title (3.4 Proper distance) /OUT pdfmark