要回答您的问题,请考虑以下 CSV 文件中的示例
bookId,bookname,description,authorname
1,Grammer,book that has details about grammer,author1
2,Special Characters, book that describes about some escape characters like \", punctuations and special characters ,etc.,author2
3,Mathematics, book that has mathematical operations like addition +, subtraction -, multiplication *, division / etc, author3
Run Code Online (Sandbox Code Playgroud)
我创建了一个简单的工作,如下所示
在上面的示例中,字符逗号","
是分隔符。但数据之间有一些逗号。
写入 CSV 文件的数据如下所示,
现在,当我从该文件读取数据时,我将得到以下数据
.------+------------------+-------------------------------------------------------+-------------------------------------.
| tLogRow_3 |
|=-----+------------------+-------------------------------------------------------+------------------------------------=|
|bookId|bookName |description |author |
|=-----+------------------+-------------------------------------------------------+------------------------------------=|
|1 |Grammer |book that has details about grammer |author1 |
|2 |Special Characters|book that describes about some escape characters like "| punctuations and special characters |
|3 |Mathematics |book that has mathematical operations like addition + | subtraction - |
'------+------------------+-------------------------------------------------------+-------------------------------------'
Run Code Online (Sandbox Code Playgroud)
如果您注意到,列的日志中缺少一些数据"author"
。
这是因为数据之间有逗号。为了避免这种情况,Text Enclosure
使用了选项。数据中还有一个转义字符,即\"
。在文件中它将被打印为"
. 如果Text Enclosure
值为"""
,那么您需要转义"
数据中存在的字符。为此,您必须使用Escape char
选项,如下所示
现在我得到的输出是
当我读取这些数据时,我会得到如下数据,
.------+------------------+-------------------------------------------------------------------------------------------------------+-------.
| tLogRow_3 |
|=-----+------------------+-------------------------------------------------------------------------------------------------------+------=|
|bookId|bookName |description |author |
|=-----+------------------+-------------------------------------------------------------------------------------------------------+------=|
|1 |Grammer |book that has details about grammer |author1|
|2 |Special Characters|book that describes about some escape characters like ", punctuations and special characters ,etc. |author2|
|3 |Mathematics |book that has mathematical operations like addition +, subtraction -, multiplication *, division / etc.|author3|
'------+------------------+-------------------------------------------------------------------------------------------------------+-------'
Run Code Online (Sandbox Code Playgroud)
如果您注意到,则不会丢失任何数据。
希望这会对您有所帮助。
归档时间: |
|
查看次数: |
9799 次 |
最近记录: |