当前位置: 首页 > wzjs >正文

包装建设网站服务器域名查询

包装建设网站,服务器域名查询,东莞外贸公司网站制作,长春火车站什么时候通车使用pybind11开发供Python项目使用的C扩展模块时,如果在扩展模块的C代码中向控制台输出的信息中包含中文,python程序的控制台很容易出现乱码。以如下C扩展框架代码为例(这是对上一篇文章简明使用pybind11开发pythonc扩展模块教程-CSDN博客中的…

使用pybind11开发供Python项目使用的C++扩展模块时,如果在扩展模块的C++代码中向控制台输出的信息中包含中文,python程序的控制台很容易出现乱码。以如下C++扩展框架代码为例(这是对上一篇文章简明使用pybind11开发pythonc+扩展模块教程-CSDN博客中的C++扩展框架代码进行少量修正后的结果):

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <locale>
#include <codecvt>
#include <windows.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>namespace py = pybind11;class CSVFinder {
private:std::map<std::string, std::vector<std::string>> dataMap;std::vector<std::string> headers;public:// 默认构造函数CSVFinder() {}// 接受 CSV 文件路径的构造函数CSVFinder(const std::string& filePath) {loadCSV(filePath);}// 载入 CSV 文件的方法void loadCSV(const std::string& filePath) {// 检查文件扩展名是否为 .csvif (filePath.substr(filePath.find_last_of(".") + 1) != "csv") {std::cerr << "文件扩展名不是 .csv,但仍尝试解析: "  << filePath << std::endl;}std::ifstream file(filePath);if (!file.is_open()) {std::cerr << "无法打开文件,请检查文件名或路径是否错误: " << filePath <<  std::endl;dataMap.clear();headers.clear();return;}std::string line;// 读取第一行作为标题if (!std::getline(file, line)) {std::cerr << "无法读取文件的第一行,请检查文件内容: " << filePath << std::endl;dataMap.clear();headers.clear();return;}std::istringstream iss(line);std::string token;while (std::getline(iss, token, ',')) {headers.push_back(token);}if (headers.empty()) {std::cerr << "第一行未包含有效的标题信息,请检查文件内容: "<< filePath << std::endl;dataMap.clear();headers.clear();return;}// 读取后续行while (std::getline(file, line)) {std::istringstream iss(line);std::vector<std::string> values;std::string token;while (std::getline(iss, token, ',')) {values.push_back(token);}if (values.empty()) {std::cerr << "某行未包含有效的数据信息,请检查文件内容: "<< filePath << std::endl;dataMap.clear();headers.clear();return;}std::string key = values[0];values.erase(values.begin());dataMap[key] = values;}if (dataMap.empty()) {std::cerr << "The file does not contain valid data lines:"<< filePath << std::endl;dataMap.clear();headers.clear();return;}file.close();}// 返回 dataMap 的方法const std::map<std::string, std::vector<std::string>>& getDataMap() const {return dataMap;}// 返回 headers 的方法const std::vector<std::string>& getHeaders() const {return headers;}// 查找数据的方法py::object findData(const std::string& rowTitle, const std::string& colTitle) {auto rowIt = dataMap.find(rowTitle);if (rowIt != dataMap.end()) {for (size_t i = 1; i < headers.size(); ++i) {if (headers[i] == colTitle && i < rowIt->second.size()) {return py::cast(rowIt->second[i]);}}}return py::none();  // 返回 None 对象}// 查找行的方法std::vector<std::string> findRow(const std::string& rowTitle) {auto it = dataMap.find(rowTitle);if (it != dataMap.end()) {return it->second;}return {};}// 查找列的方法std::vector<std::string> findColumn(const std::string& colTitle) {std::vector<std::string> column;// 查找列标题在headers中的索引, 注意 headers[0] 为行标题,因此从index 1开始int colIndex = -1;for (size_t i = 1; i < headers.size(); ++i) {if (headers[i] == colTitle) {colIndex = static_cast<int>(i - 1); // 对应到每行数据中的索引break;}}if (colIndex < 0) {return column; // 未找到对应的列标题,返回空向量}// 遍历每一行数据for (const auto& row : dataMap) {const std::vector<std::string>& values = row.second;if (static_cast<size_t>(colIndex) < values.size()) {column.push_back(values[colIndex]);}else {column.push_back(""); // 如该行数据列数不足,可选择返回空字符串}}return column;}
};PYBIND11_MODULE(CSVFinder, m) {py::class_<CSVFinder>(m, "CSVFinder").def(py::init<>()).def(py::init<const std::string&>()).def("load_csv", &CSVFinder::loadCSV).def("get_datamap", &CSVFinder::getDataMap).def("get_headers", &CSVFinder::getHeaders).def("find_data", &CSVFinder::findData).def("find_row", &CSVFinder::findRow).def("find_column", &CSVFinder::findColumn);
}

其中loadCSV方法中有不少向控制台输入的错误信息。将上面的框架构建分发给Python项目使用(具体过程参见本文开头提到的博客),使用下面的python代码进行测试:

from CSVFinder import CSVFinderwidth = 8file = "E:/projects/ziweidoushu/csv1/destiny_type.csv"
finder = CSVFinder(file)
dict = finder.get_datamap()
headers = finder.get_headers()
print(f'{[value.ljust(width) + "|" for value in headers]}')
for key, values in dict.items():print(f'{key.ljust(width)  + "|" }:{[value.ljust(width)  + "|" for value in values]}')
key = '甲'
row = finder.find_row(key)
print(f"{key.ljust(width)  + '|' }:{len(row)}:{[value.ljust(width)  + '|' for value in row]}")
key = '寅'
column = finder.find_column(key)
print(f"{key.ljust(width)  + '|' }:{len(column)}:{[value.ljust(width)  + '|' for value in column]}")

测试程序中的文件路径故意写错了,本来应该向控制台输出C++代码中的包含中文的错误信息:

无法打开文件,请检查文件名或路径是否错误: E:/projects/ziweidoushu/csv1/destiny_type.csv

在控制台执行测试程序,实际输出如下图:

可以看到C++扩展模块向控制台输出的中文信息变成了乱码,但是Python程序向控制台输出的中文信息则显示正常。AI以及不少文章说用下面的命令将控制台所使用的编码改成UTF-8能够解决问题:

chcp 65001

实际上起不了作用:

实际上只要在C++扩展模块中在字符串前加上u8修饰符、在模块入口处将控制台编码改为UTF-8,并给编译器加上“/utf-8”选项即可正常显示中文,而无需调整控制台编码页。也就是:

1、在C++扩展模块代码中包含<windows.h>,然后调用Windows API在pybind11模块入口处进行如下调用即可:

//省略一些代码

 std::cerr << u8"无法打开文件,请检查文件名或路径是否错误: " << filePath << std::endl;

// 省略一些代码

PYBIND11_MODULE(CSVFinder, m) {
    SetConsoleOutputCP(CP_UTF8);  // 增加的代码
    std::cerr.imbue(std::locale("chs"));  // 增加的代码,可省略
    std::cout.imbue(std::locale("chs"));  // 增加的代码,可省略

// 省略后面的代码

实际上C++扩展模块中增加的三行代码后面两行省略也能解决问题,但考虑到提高健壮性,加上后两行代码,让控制台认为处于中文环境中。

2、在setup.py的扩展模块定义中,增加“/utf-8”选项:

# 定义扩展模块
csv_module = Extension(
    'CSVFinder',  # 模块名称
    sources=['read_csv.cpp'],  # C++ 源文件路径
    include_dirs=[pybind11.get_include(), ],  
    language='c++',  # 指定使用 C++ 语言
    extra_compile_args=['/utf-8','-D_WIN32_WINNT=0x0601', '-D__USE_MINGW_ANSI_STDIO=1'],  # 编译选项
)

重新构建并测试,结果如下:


文章转载自:

http://UdRExR3f.qsmdd.cn
http://Jvxz4QCC.qsmdd.cn
http://mhTIy1uW.qsmdd.cn
http://bPoP20GD.qsmdd.cn
http://1EtaSg1S.qsmdd.cn
http://rQzeo4xw.qsmdd.cn
http://yD51OaF4.qsmdd.cn
http://IriVwQj7.qsmdd.cn
http://qxg5TP8Q.qsmdd.cn
http://LgaO6Y5i.qsmdd.cn
http://ArviOJ23.qsmdd.cn
http://fT0S2GkS.qsmdd.cn
http://0BR9j3aB.qsmdd.cn
http://qovHMxN3.qsmdd.cn
http://bCAI8SWm.qsmdd.cn
http://jNoB5s5D.qsmdd.cn
http://TdNLDPOY.qsmdd.cn
http://Z2rf2MGs.qsmdd.cn
http://giBIq2uT.qsmdd.cn
http://FqrUcxOG.qsmdd.cn
http://te0k0Jfg.qsmdd.cn
http://DYetiFQt.qsmdd.cn
http://RrD9sUF5.qsmdd.cn
http://RiCgJOdo.qsmdd.cn
http://9LUvPOys.qsmdd.cn
http://Qs4Om2LC.qsmdd.cn
http://btFFS2xC.qsmdd.cn
http://nioqYe0G.qsmdd.cn
http://Br6JHXZY.qsmdd.cn
http://ou8fg1c7.qsmdd.cn
http://www.dtcms.com/wzjs/611676.html

相关文章:

  • 建网站花多少钱如何让别人看到自己做的网站
  • 看电影免费网站网站建设宣传页
  • 广东省建设交易中心网站陶哲轩博客wordpress
  • 广西网站建设原创wordpress图片美化
  • 甘南州住房和城乡建设局网站什么网页游戏最火
  • 怎么做全息网站上饶做网站公司
  • 宜昌做网站要什么条件和国外做贸易用什么网站
  • 大连自媒体公司网站优化流程图
  • 欧美做的爱爱网站网页设计用什么尺寸的画布
  • 郑州公司网站开发标准营销型网站定做价格
  • 那些行业做网站优化的比较多中国造价工程建设监理协会网站
  • python可以做网站企业网站建设方案详细方案
  • 台州网站建设系统怎样在文章后做网站链接
  • 做网站素材网外贸公司网站设计哪家好
  • 医院网站建设招标国外flash网站模板
  • 温州网站的建设用wordpress建站难吗
  • 网站建设合同司法解释太原网站建设方案咨询
  • 建设部网站施工合同抖音自媒体平台注册
  • 海口网站设计保定网站建设求职简历
  • 电商网络营销seo排名哪家有名
  • 织梦做的网站进不去分分作网站
  • 网站脚本错误深圳互联网设计开发
  • 网文订阅做多的网站国家工商注册网
  • 网站制作合同模板建筑工程网官网入口
  • 任丘做网站wordpress是什么平台
  • 建站公司经营seo培训班
  • 怎么制作游戏短视频临沂 网站优化
  • win2012服务器做网站wordpress修改发布页面插件
  • 昆山网站建设推荐北京网站建设推广
  • 网站建设赶集网韩国优秀平面设计网站有哪些