如何在Groovy中转换描述表输出列表或映射

时间:2018-08-27 06:43:06

标签: groovy

如何在groovy中转换以下输出地图或列表

    import React, { Component } from 'react';
import { AppRegistry, View,Image } from 'react-native';

export default class FlexDirectionBasics extends Component {
  render() {
    return (
      // Try setting `flexDirection` to `column`.
      <View style={{flex: 1, }}>
         <View style={{flex: 1, flexDirection: 'row', }}>
        <Image source={{uri: 'https://facebook.github.io/react/logo-og.png'}}
       style={{flex:1}} />
        <Image source={{uri: 'https://cdn-images-1.medium.com/max/512/1*qUlxDdY3T-rDtJ4LhLGkEg.png'}}
       style={{flex:1}} />

      </View>
  <View style={{flex: 1, flexDirection: 'row', }}>
      <Image source={{uri: 'https://rawgit.com/gorangajic/react-icons/master/react-icons.svg'}}
       style={{flex:1}} />

        <Image  source={{uri: 'https://facebook.github.io/react/logo-og.png'}}
       style={{flex:1}} />

      </View>
      </View>
    );
  }
};

// skip this line if using Create React Native App
AppRegistry.registerComponent('AwesomeProject', () => FlexDirectionBasics);
  • 需要在单独的col_name,data_type,comment "brand","string","" "tactic_name","string","" "tactic_id","string","" "content_description","string","" "id","bigint","" "me","bigint","" "npi","bigint","" "fname","string","" "lname","string","" "addr1","string","" "addr2","string","" "city","string","" "state","string","" "zip","int","" "event","string","" "event_date","timestamp","" "error_flag","string","" "error_reason","string","" "vendor","string","" "year","int","" "month","int","" "",, "# Partition Information",, "# col_name ","data_type ","comment " "",, "vendor","string","" "year","int","" "month","int",""** 中将分区 columns和单独的map中的 normal 列分开。

预期输出:

map

1 个答案:

答案 0 :(得分:0)

尝试此代码:

CsvParser用于阅读文本。但是您的文本在解析之前需要进行一些更改。因此,我做了一些text processing使其适合csv格式。

@Grab('com.xlson.groovycsv:groovycsv:0.2')
import com.xlson.groovycsv.CsvParser

def csv = '''col_name,data_type,comment
"brand","string",""
"tactic_name","string",""
"tactic_id","string",""
"content_description","string",""
"id","bigint",""
"me","bigint",""
"npi","bigint",""
"fname","string",""
"lname","string",""
"addr1","string",""
"addr2","string",""
"city","string",""
"state","string",""
"zip","int",""
"event","string",""
"event_date","timestamp",""
"error_flag","string",""
"error_reason","string",""
"vendor","string",""
"year","int",""
"month","int",""
"",,
"# Partition Information",,
"# col_name            ","data_type           ","comment             "
"",,
"vendor","string",""
"year","int",""
"month","int",""**'''


def maptxt = csv.split('"# Partition Information",,')

def map1txt = maptxt[0].trim()

def map2txt = maptxt[1].trim().readLines().collect{ 

        it=it.replace('#','')

        it=it.replaceAll("\\s", "")

}.join('\n')

println getAsMap(map1txt)

println getAsMap(map2txt)



Map getAsMap (def txt)

{
    Map ret = [:]

    def data = new CsvParser().parse(txt)

    for (each in data){

        if(each.col_name) // empty keys are neglected.

        ret[each.col_name]=each.data_type
    }

    ret

}

您的文本中有空的col_name。这段代码忽略了这些行。