C 中開啟TXT檔案轉換成JSON檔案

2021-10-05 04:21:02 字數 1544 閱讀 7315

void

changetojson()

while

(txt[end]

!=' '

&&txt[end]

!='\n'

&& end < txt.

size()

)

string temp = txt.

substr

(pos, end-pos)

; info[k++

]= temp;

txt = txt.

substr

(end);}

if(txt[0]

=='\n'

)#pragma endregion #pragma region 型別轉換以及把string陣列的資料放到json陣列物件中去

istringstream is

(info[2]

);//構造輸入字串流,流的內容初始化為info[2]的字串

int i;

is >> i;

//從is流中讀入乙個int整數存入i中

info[

"classname"

]= json::

value

(info[0]

);

info[

"studentname"

]= json::

value

(info[1]

);

info[

"score"

]= json::

value

(i);

root[

"studentinfo"].

( json::

value

(info)

);

info[0]

.clear()

; info[1]

.clear()

; info[2]

.clear()

;#pragma endregion

if(txt.

empty()

)break;}

#pragma region 輸出檔案

json::streamwriterbuilder builder;

std::unique_ptr

;writer

(builder.

newstreamwriter()

);//json::streamwriter*

writer

(builder.

newstreamwriter()

);std::ofstream os

("./txt_json.json");

writer-

>

write

(root,

&os)

; os.

close()

;#pragma endregion

}

將Txt檔案轉換成dataset

今天工作中,遇到需要將txt檔案轉化到記憶體表dataset中,於是寫了乙個方法來實現 txt檔案是特定格式的,檔案中,一條記錄為一行,各欄位之間用 分割 注 最後乙個欄位後,有 各欄位依次為資料庫中的相應字段。舉例如下 id號 線路編碼 車站 編碼 執行模式 模式設定日期 模式設定時間 1 98 ...

python將txt檔案轉換成csv

直接上 coding utf 8 import pandas as pd import configparser import csv from py2neo import graph,node,relationship import urllib3 urllib3.disable warnings...

Python 把txt檔案轉換成csv

最近在專案上需要批量把txt檔案轉成成csv檔案格式,以前是手動開啟excel檔案,然後匯入txt來生產csv檔案,由於這已經變成每週需要做的事情,決定用python自動化指令碼來實現,思路 讀取資料夾中所有txt檔案,儲存到list中 針對每個txt檔案,自動生產同檔名的csv檔案 對每個txt檔...