C 解析csv檔案

2022-07-26 17:18:12 字數 1792 閱讀 4673

開頭不留空,以行為單位;

可含或不含列名,含列名則居檔案第一行;

一行資料不跨行,無空行;

以半形逗號(即,)作分隔符,列為空也要表達其存在;

列內容如存在半形逗號(即,),則用半形雙引號("")將該字段值包含起來;

列內容如存在半形雙引號(即"),則用兩個雙引號("")將其替換,再用半形雙引號引號(即"")將該字段值包含起來;

檔案讀寫時引號,逗號操作規則互逆;

內碼格式不限,可為 ascii、unicode 或者其他;

不支援特殊字元。

csv檔案的生成,最簡單的即為:excel->檔案->另存為(.csv) 

根據規則可知,csv的每一行資料的引號個數必定為偶數個,每行中的每項資料個數必定為偶數個,即不可能存在雙引號個數為奇數個的資料項,故可據此編寫解析csv**:

using

system;

using

system.collections;

using

system.text;

using

system.io;

using

system.collections.generic;

using

system.text.regularexpressions;

public

class

csvhelper

private

static

int _getdoublequotacount(string

str)

/*** csv的每一行的每一項的引號個數必定為偶數

* 生成的dictionary>以每一列的第一行元素作為key,其它元素的集合作為value

*/public

static dictionary> analysiscsvbystr(string

csvinfo)

string lineinfoarray = infolines[i].split(','

); list

rowitemlist = new list();

string strtemp = string

.empty;

for (int j = 0; j < lineinfoarray.length; j++)

}else

rowitemlist.add(strtemp);

strtemp = string

.empty;}}

if (i == 0

)

}int indextemp = 0

;

for (; indextemp < rowitemlist.count; indextemp++)

itemlistarray[indextemp].add(rowitemlist[indextemp]);

}if (indextemp < itemlistarray.length - 1

)

}for (int i = 0; i < itemlistarray.length; i++)

return

csvinfodic;

}public

static dictionary> analysiscsvbyfile(string

csvpath)

else}}

解析csv檔案

以下內容僅為記錄 第一步 解析csv檔案 test public void testcsv throws exception catch filenotfoundexception e string line string everyline try system.out.println csv 中...

CSV檔案解析

實現功能 針對讀取的csv文件字串進行map格式輸出 主要 如下 public class csvpaser extends parser public static void main string args system.out.println map 說明 該部分主要針對多條記錄做相應的解析 ...

ios解析csv檔案

nsstring path nsbundle mainbundle pathforresource citys oftype csv nsstringencoding gbkencoding cfstringconvertencodingtonsstringencoding kcfstringenc...