Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提供写入数据的sql语句 #127

Open
SimonFuInSky opened this issue May 27, 2024 · 1 comment
Open

提供写入数据的sql语句 #127

SimonFuInSky opened this issue May 27, 2024 · 1 comment

Comments

@SimonFuInSky
Copy link

SimonFuInSky commented May 27, 2024

非常感谢提供详尽的数据。
我写了一段python代码,可以提取目录中型号以及手机名称对应关系,生成sql语句到数据库中,方便大家使用。

import os

# 定义目录路径
directory_path = 'D:\\project\\github\\MobileModels\\brands'  # 替换成你的md文件目录路径
output_file = os.path.join(directory_path, 'insert.sql')

# 存储匹配结果
matches = []

# 打开日志文件
log_file_path = os.path.join(directory_path, 'processing.log')
with open(log_file_path, 'w', encoding='utf-8') as log_file:

    # 遍历目录中的所有md文件
    for filename in os.listdir(directory_path):
        if filename.endswith('.md'):
            log_file.write(f'Processing file: {filename}\n')
            print(f'Processing file: {filename}')
            with open(os.path.join(directory_path, filename), 'r', encoding='utf-8') as file:
                for line in file:
                    line = line.strip()
                    if line.startswith('`') and ':' in line:
                        parts = line.split(':')
                        user_agent_device_id = parts[0].strip('`')
                        user_agent_device_name = ':'.join(parts[1:]).strip()
                        matches.append((user_agent_device_id, user_agent_device_name))
                        log_file.write(f'Matched: {user_agent_device_id}, {user_agent_device_name}\n')

# 生成SQL插入语句
with open(output_file, 'w', encoding='utf-8') as file:
    file.write('USE your_database_name;\n')  # 替换成你的数据库名称
    file.write('INSERT INTO user_agent_device_model (user_agent_device_id, user_agent_device_name) VALUES\n')
    
    values = []
    for match in matches:
        user_agent_device_id, user_agent_device_name = match
        values.append(f"('{user_agent_device_id}', '{user_agent_device_name}')")
    
    file.write(",\n".join(values) + ";\n")

print(f'SQL insert statements have been written to {output_file}')
print(f'Processing log has been written to {log_file_path}')
@SimonFuInSky
Copy link
Author

截止2024-05-27生成好的sql文件
insert.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant