今天因为需要帮一个同事的新闻内容录入为html, 每次手改不方便,所以就弄了个csv(excel)转json的c++程序,然后再利用ejs把它渲染成网页,打开渲染好的网页再保存(不能保存源文件,不然还是空的),就可以把内容弄成一个html了,此作mark,以下为程序

// convert.cpp
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main() {
freopen("in.csv", "r", stdin);
freopen("json.js", "w", stdout);
string str, s[];
int g = ;
int status = ;
char ch;
str = "";
while((ch = cin.get()) != EOF) {
str += ch;
}
for(int i = ; i < str.size(); i++) {
if(status == ) {
if(str[i] == '\"') {
status = ;
} else {
s[g] += str[i];
status = ;
}
} else if(status == ) {
if(str[i] == '\"') {
if(i + >= str.size()) {
g++;
break;
} else if(str[i + ] == '\"') {
s[g] += '\"';
++i;
} else if(str[i + ] == ',') {
status = ;
++g;
++i;
} else if(str[i + ] == '\n') {
status = ;
++g;
++i;
}
} else {
s[g] += str[i];
}
} else if(status == ) {
if(str[i] == ',') {
++g;
} else if(str[i] == '\n') {
++g;
} else {
s[g] += str[i];
}
}
}
int x = ;
puts("var data = [");
string name[] = {"title", "detail", "img", "url"};
for(int i = ; i < g; i++) {
if(x == ) {
if(i == ) {
puts("\t{");
} else {
puts("\t},");
puts("\t{");
}
}
cout << "\t\t" << name[x] << ": \"" << s[i] << "\"" << (x == ? "" : ",") << endl;
x = (x + ) % ;
}
puts("\t}");
puts("]");
return ;
}

csv转json文件的更多相关文章

  1. 一文综述python读写csv xml json文件各种骚操作

      Python优越的灵活性和易用性使其成为最受欢迎的编程语言之一,尤其是对数据科学家而言.这在很大程度上是因为使用Python处理大型数据集是很简单的一件事情. 如今,每家科技公司都在制定数据战略. ...

  2. postman-----使用CSV和Json文件实现批量接口测试

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px ".PingFang SC"; color: #454545 } span ...

  3. csv、json 文件读取

    1.CSV 文件存储 1.1 写入 简单示例 import csv with open('data.csv', 'a') as csvfile: writer = csv.writer(csvfile ...

  4. python读写word、excel、csv、json文件

    http://blog.csdn.net/pipisorry/article/details/50368044 python读写word文档 (include wps)将word文档转换成txt文档 ...

  5. pyspark 读写csv、json文件

    from pyspark import SparkContext,SparkConf import os from pyspark.sql.session import SparkSession de ...

  6. 转载:python生成以及打开json、csv和txt文件

    原文地址:https://blog.csdn.net/weixin_42555131/article/details/82012642 生成txt文件: mesg = "hello worl ...

  7. python 将json格式的数据写入csv格式的文件中

    # coding=utf-8 import json import csv # 重新进行配置读写数据时的默认编码 import sys reload(sys) sys.setdefaultencodi ...

  8. mongodb-导出数据到csv文件或json文件

    在mongodb的bin目录下, 有一个mongoexport, 可用于数据的导出 [wenbronk@localhost bin]$ ./mongoexport --help Usage: mong ...

  9. 爬虫文件存储:txt文档,json文件,csv文件

    5.1 文件存储 文件存储形式可以是多种多样的,比如可以保存成 TXT 纯文本形式,也可以保存为 Json 格式.CSV 格式等,本节我们来了解下文本文件的存储方式. 5.1.1 TXT文本存储 将数 ...

随机推荐

  1. c#_delegate_异步调用_BeginInvoke

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. Understanding the Router

    Understanding the Router Our module is coming along nicely. However, we're not really doing all that ...

  3. git 常用命令 创建查看删除分支,创建查看删除tag等

      1. git 文档 https://github.com/progit/progit/blob/master/zh/02-git-basics/01-chapter2.markdown https ...

  4. 关于IE调试模式下才能显示效果

    要去除console.log() 低版本IE 没有开启调试模式  console.log()会导致报错

  5. git 配置用户名和邮箱

    在安装了git for windows之后,个人总是忘记配置git config的命令,以此记录一下: 配置用户名和邮箱的命令 git config --global user.name " ...

  6. div中的img垂直居中

    <html> <head> <style type="text/css"> .imgDiv { overflow: hidden; displa ...

  7. 获取iframe 内元素的方法

    1,原生的方法 首先给iframe 设置 id 属性 var obj = document.getElementById('iframe').contentWindow; setTimeout(fun ...

  8. JAXB - Annotations, Class Fields as Attributes: XmlAttribute

    Provided that XML lets you represent a data item as a single value, there is no cut-and-dried rule f ...

  9. Acitivity间数据的传递

    使用startActivityForResult方法进行数据传递.    MainActivity.java: public class MainActivity extends Activity { ...

  10. 在有大量数据时 少用In(数据会丢失) 用left join 代替

    select @From, @To, EffectiveDate, GETDATE(), Rate from Config_Currency_ExchangeRate_Temp where Effec ...