应用于各种文件解析或截取的方法(C语言和Python)

在工作过程中,我们会遇到很多文件需要由源文件中提取出需要的数据,根据各种关键词进行提取。
废话不多说,直接上代码。

python版本:

import struct
import os

# 自定义方法
def find_string_in_file(file_path_1, file_path_2, search_string):
    with open(file_path_1, 'r') as file:
        content = file.read()
        
    file_2 = open(file_path_2, "w")
    start_index = 0
    first_index = 31	#search_string的长度或者需要截取的字符串的起始位置
    get_index = 167		#需要截取的字符串的结束位置
    number = 0			#记录查询到search_string的次数(循环次数)
    while True:
        index = content.find(search_string, start_index)
        if index == -1:
            break
        #这里的replace函数可用于替换截取字符串中需要替换或者删除的字符
        str_1 = content[index+31:index+167].replace(" ", "").replace("\n", "")	
        file_2.write(str_1+"\n")
        start_index = index + len(search_string)
        number = number + 1    
        
# 使用示例
file_path_1 = 'xxxxx'  # 替换为你的文件路径
file_path_2 = 'xxxxx'	#将解析的数据存放到该文件
search_string = 'xxxxx'  # 替换为你想要查找的字符串
find_string_in_file(file_path_1, file_path_2, search_string)

C语言版本:

#include <stdio.h>
#include <string.h>

#define BUF_SIZE    300000000	//读取文件内容大小
char content[BUF_SIZE];		//该字符数组最好定义为全局,否则容易形成数组溢出

//自定义方法
void find_string_in_file(const char* file_path_1, const char* file_path_2, const char* search_string) {
    FILE* file = fopen(file_path_1, "r");
    if (file == NULL) {
        printf("error: could not open file %s\n", file_path_1);
        return;
    }
    FILE* file_2 = fopen(file_path_2, "w");
    if (file_2 == NULL) {
        printf("error: could not open file %s\n", file_path_2);
        return;
    }
    
    size_t number = 0;	//该变量用于记录查询次数
    memset(content, 0, BUF_SIZE);	//格式化字符数组
    size_t start_index = 0;
    size_t content_index = (size_t)content;	//将定义字符数组的地址
    size_t len = fread(content, sizeof(char), BUF_SIZE, file);	//读取文件全部内容
    while(1)
    {
        size_t index = (size_t)strstr(content + start_index, search_string);	//用于截取文件内容
        if (index == 0) {
            break;
        }
        char str_1[137];	//用于存放截取到的文件内容
        char str_2[60];		//用于存放处理后的截取内容
        int size = 0;		//用于字符数组计数
        strncpy(str_1, content + (index-content_index) + 31, 137);	//用于字符数组内容的复制
       	//在此处对截取到的内容进行处理
        for (int j = 0; j < 137; j++)
        {
            if ((str_1[j] == ' ') || (str_1[j] == '\n'))
            {
                if (size == 59)
                {
                    str_2[size] = '\n';
                }
                continue;
            }
            else
            {
                str_2[size] = str_1[j];
                size++;
            }
        }
        fprintf(file_2, "%s", str_2);	//将处理好的内容存放到文件中
        start_index = (index-content_index) + strlen(search_string);	//为截取下一个字符串的位置索引赋值
        number++;
    }
    fclose(file);
    fclose(file_2);
}

int main() {
    const char* file_path_1 = "xxxxx"; // 替换为你的文件路径
    const char* file_path_2 = "xxxxx"; // 替换为你想要写入的文件路径
    const char* search_string = "xxxxx"; // 替换为你想要查找的字符串
    find_string_in_file(file_path_1, file_path_2, search_string);
    return 0;
}

上述用于各种文件解析或截取的方法,还需要根据自己需要解析的文件进行相应的修改。

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值