亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

Create a CSV file

系統 2541 0

Create a CSV file | Groovy Examples

Create a CSV file

For this example, let’s assume we have an array of maps as our data.

            
              def
            
             planets 
            
              =
            
            
              [
            
            
              [
            
            id:
            
              '1'
            
            ,color:
            
              'red'
            
            ,planet:
            
              'mars'
            
            ,description:
            
              'Mars, the "red" planet'
            
            
              ]
            
            ,
    
            
              [
            
            id:
            
              '2'
            
            ,color:
            
              'green'
            
            ,planet:
            
              'neptune'
            
            ,description:
            
              'Neptune, the "green" planet'
            
            
              ]
            
            ,
    
            
              [
            
            id:
            
              '3'
            
            ,color:
            
              'blue'
            
            ,planet:
            
              'earth'
            
            ,description:
            
              'Earth, the "blue" planet'
            
            
              ]
            
            ,

            
              ]
            
          

Next, we can create our CSV file as follows:

            
              def
            
             out 
            
              =
            
            
              new
            
            
              File
            
            
              (
            
            
              'planets.csv'
            
            
              )
            
            
planets.
            
              each
            
            
              {
            
            
              def
            
             row 
            
              =
            
            
              [
            
            it.
            
              id
            
            , it.
            
              color
            
            , it.
            
              planet
            
            
              ]
            
            
    out.
            
              append
            
             row.
            
              join
            
            
              (
            
            
              ','
            
            
              )
            
            
    out.
            
              append
            
            
              '
              
                \n
              
              '
            
            
              }
            
          

Which creates a file with the following content:

        1,red,mars
2,green,neptune
3,blue,earth

      

However, this does not contain our description field. If you need to also handle double quotes and commas in your data then you can:

  1. qualify your values with a double quote, and
  2. replace double quotes in your data with an escaped double quote.

In this case our code may look like:

            
              def
            
             out 
            
              =
            
            
              new
            
            
              File
            
            
              (
            
            
              'planets.csv'
            
            
              )
            
            
planets.
            
              each
            
            
              {
            
            
              def
            
             row 
            
              =
            
            
              [
            
            it.
            
              id
            
            , it.
            
              color
            
            , it.
            
              planet
            
            , it.
            
              description
            
            
              ]
            
            
                        .
            
              collect
            
            
              {
            
            
              '"'
            
            
              +
            
             it.
            
              replace
            
            
              (
            
            
              '"'
            
            ,
            
              '""'
            
            
              )
            
            
              +
            
            
              '"'
            
            
              }
            
            
    out.
            
              append
            
             row.
            
              join
            
            
              (
            
            
              ','
            
            
              )
            
            
    out.
            
              append
            
            
              '
              
                \n
              
              '
            
            
              }
            
          

Which creates a file with the following content:

        "1","red","mars","Mars, the ""red"" planet"
"2","green","neptune","Neptune, the ""blue"" planet"
"3","blue","earth","Earth, the ""blue"" planet"
      

Create a CSV file


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日日骑夜夜骑 | 亚洲成a人一区二区三区 | 九九影音 | 亚洲国产高清一区二区三区 | 成人欧美一区二区三区黑人免费 | 日本精品久久久久中文字幕2 | 精品久久久久久中文字幕一区 | 久久激情影院 | 久久精品国产亚洲婷婷 | 国产做爰免费视频观看 | 色婷婷精品综合久久狠狠 | 四虎网址换成什么了2021 | 99久久免费精品高清特色大片 | 欧美日本中文 | 秘密影院久久综合亚洲综合 | 国产日产欧产麻豆精品精品推荐 | 一区二区三区免费视频观看 | 青青青青久久国产片免费精品 | 亚洲精品不卡视频 | 亚洲一级片免费 | 不卡无毒免费毛片视频观看 | 亚洲成在人天堂一区二区 | 日日舔夜夜操 | 伊人骚| 久久久久久久综合色一本 | 大乳女做爰中文字幕 | 天天躁狠狠躁狠狠躁夜夜躁 | 99re这里只有精品在线观看 | 欧美亚洲大片 | 国产福利小视频在线观看 | 麻豆狠色伊人亚洲综合网站 | 久久久久久久影院 | 99久久精品国产片久人 | 99这里 | 久久中文字幕在线观看 | 一级特级片| 久草在线视频在线观看 | 337p色噜噜 | 亚洲一区二区天海翼 | 伊人色综合网一区二区三区 | 亚洲毛片免费视频 |