CSVを表に変換


15

チャレンジ

CSV入力が与えられたら、ボックス文字を使用して適切なUnicodeテーブルを出力します。

書式設定

テーブルは、次のルールを使用してフォーマットされます。

  • 列幅はその列の最長値に等しくなります
  • すべてのテーブルデータは左揃えになります
  • 各テーブルでは、最初のcsv行がヘッダーであると想定されます
  • テーブルでは、境界に次の文字が使用されます。

┌ ┬ ┐ ├ ┼ ┤ └ ┴ ┘ ─ │

Input:
Name,Age,Gender
Shaun,19,Male
Debra,19,Female
Alan,26,Male
George,15,Male

Output:
┌──────┬───┬──────┐
│Name  │Age│Gender│
├──────┼───┼──────┤
│Shaun │19 │Male  │
│Debra │19 │Female│
│Alan  │26 │Male  │
│George│15 │Male  │
└──────┴───┴──────┘

ルール

  • 標準的な抜け穴が適用されます
  • 完全なプログラム、関数、またはラムダを提出できます
  • 入力は、ファイル、プログラム引数、または任意の受け入れ可能な代替からのものです。
  • 出力は、ファイル、返されたもの、または受け入れ可能な代替物になります。
  • CSV入力は、私の例で使用したものと同じ形式を取る必要があります。
  • バイト単位の最短回答が優先されます。

CSV入力は次の形式を取る必要があります。

Header1,Header2,Header3 newline
Column1,Column2,Column3 newline
Column1,Column2,Column3 optional_newline

2
CSV定義を使用するには、基本的に2つの方法があると思います。問題の興味深い部分が出力である場合、「カンマで分割する」のように簡単にでき、カンマの引用方法や引用文字の引用方法を心配する必要がありません。そうでない場合は、CSVを解析する特定の方法を述べることができます(「二重引用符は、カンマを無視するモードを切り替えます。2つの二重引用符が連続してリテラルの二重引用符を生成します」」実在する)。

4
エラー、深刻な問題:勝利条件を指定していません。最適化されるプログラムは何ですか?長さ(code-golf)?

1
少なくともそこにある最初の3つのリンクはすべて、CSVを異なる方法で定義しています(少なくとも2つは、CSVを実行する方法がたくさんあると言っています)。したがって、質問で使用するために「CSV」をより完全に定義する必要があると仮定しています(また、ソリューションは、コンマでの分割を回避し、短くできるためエスケープを処理しません)。

2
さて、質問を編集して、誰もが使用したいCSV形式の詳細を含めました。
ショーンワイルド

1
CRLF?マジ?これはUnixでかなり大きなペナルティを与えます。CRはテキストファイル内の何かを意味します。おそらくそれを「改行」に置き換えて、OS固有の改行を使用できるようにする必要があるでしょう。

回答:


10

Try(Dyalog)APL38 43 バイト

最後の入力行には末尾の改行が必要です。

{{(⊃⍵)⍪⍉⍪↑¨↓⍉↑1↓⍵}s¨',',¨(s1↓¨⊢⊂⍨⊢=⊃)¯1⌽⍵}

オンラインでお試しください!Dyalog APLのオフラインバージョンで]boxing ON -style=min、同じ効果を実行します。

説明

{... 引数を表す}匿名関数

¯1 ⌽ ⍵ 末尾の改行を前に回転します

(s ←... )関数sを次のように定義し、適用します

  1 ↓¨ それぞれの最初の文字をドロップします

  ⊢ ⊂⍨ 行、どこで分割

  ⊃ = ⊢ 最初の文字は文字列の文字と等しい

',' ,¨ 次に、各行にカンマを追加します

関数sを各行に適用します

{... }次の匿名関数を適用します。

  1 ↓ ⍵ 最初の要素(行ヘッダー)をドロップします

  ↓ ⍉ ↑ 行のリストを列のリストに転置する

  ↑¨ 各要素(エントリのリスト)をパディングされたエントリのマトリックスにします

  ⍉ ⍪ 1列の行列を作成してから、1行の行列に転置する

  (⊃⍵) ⍪ 引数の最初の要素(ヘッダーのリスト)を先頭に配置します `

注:線画の文字は私のソリューションでは明示的に使用されていませんが、APL文字セットの一部であり、1バイトとしてもカウントされます。


上記のコメントを参照Is input using list or array of strings (and no newlines) valid? Nope.
-edc65

@ edc65修正済み。ありがとう。
アダム

ああ、その箱入りディスプレイは確かに便利です:)
ヴェン

2

PowerShell 3 +、365バイト

$d=$input|ipcsv
$h=$d[0].PSObject.Properties.Name|%{$_|Add-Member -type NoteProperty -na c -v(($d.$_+$_|measure Length -ma).Maximum)-pa}
"┌$(($h|%{'─'*$_.c})-join'┬')┐"
"│$(($h|%{$_.PadRight($_.c)})-join'│')│"
"├$(($h|%{'─'*$_.c})-join'┼')┤"
$d|%{$i=$_;"│$(($h|%{$i.$_.PadRight($_.c)})-join'│')│"}
"└$(($h|%{'─'*$_.c})-join'┴')┘"

これは大幅に改善されると思いますが、時間が足りませんでした。すべての行末には\nnoがあり\r、エンコーディングはBOMなしのUTF8です。


1

ラケット578バイト

(let*((ll(map(λ(x)(string-split x","))ll))(lr list-ref)(sl string-length)(d display)(dl displayln)(nc(length(lr ll 0)))
(nl(for/list((i nc))(apply max(for/list((j ll))(sl(lr j i))))))(pl(λ(sy)(d(lr sy 0))(for((n nc))(for((m(lr nl n)))(d(lr sy 1)))
(if(< n(sub1 nc))(d(lr sy 2))(dl(lr sy 3))))))(g(λ(i n)(for((m(-(lr nl n)(sl i))))(d" ")))))(pl'("┌""─""┬""┐"))
(for((i(lr ll 0))(n(in-naturals)))(d"│")(d i)(g i n))(dl"│")(pl'("├""─""┼""┤"))(for((j(range 1(length ll))))
(for((i(lr ll j))(n nc))(d"│")(d i)(g i n))(dl"│"))(pl'("└" "─" "┴" "┘")))

ゴルフをしていない:

(define(f1 ll)
 (let* ((ll (map (λ (x)(string-split x ",")) ll))  ; use this to convert csv format to list of lists; 
         (lr list-ref)                    ; make short names of standard fns
         (sl string-length)
         (d display)
         (dl displayln)
         (nc (length (lr ll 0)))          ; number of cols; 
         (nl(for/list ((i nc))            ; get list of max string-length for each column
              (apply max
                     (for/list ((j ll))
                       (sl (lr j i))
                       ))))
         (pl (λ (sy)                      ; put lines using sent symbol list
               (d (lr sy 0)) 
               (for ((n nc))
                 (for ((m (lr nl n))) (d (lr sy 1)))
                 (if (< n (sub1 nc))
                     (d (lr sy 2))
                     (dl (lr sy 3))
                     ))))
         (g (λ (i n)                     ; pad with spaces if needed
              (for ((m (- (lr nl n) (sl i)))) (d " ")) ))) 
    ; put line above header: 
    (pl '("┌" "─" "┬" "┐"))

    ; put header: 
    (for ((i (lr ll 0)) (n (in-naturals)))
      (d "│")
      (d i)
      (g i n)
      )
    (dl "│")

    ; put line below header;
    (pl '("├" "─" "┼" "┤"))

    ; put rows: 
    (for ((j (range 1 (length ll))))
      (for ((i (lr ll j))
            (n nc))
        (d "│")
        (d i)
        (g i n)
        )
      (dl "│")
      )

    ; put bottom line: 
    (pl '("└" "─" "┴" "┘"))
    ))

テスト:

(f (list  "Name,Age,Gender"
          "Shaun,19,Male"
          "Debra,19,Female"
          "Alan,26,Male"
          "George,15,Male"))

出力:

┌──────┬───┬──────┐
│Name  │Age│Gender│
├──────┼───┼──────┤
│Shaun │19 │Male  │
│Debra │19 │Female│
│Alan  │26 │Male  │
│George│15 │Male  │
└──────┴───┴──────┘

1

JavaScript(ES6 | FireFox)、286バイト

f=>(d=f.split`
`.map(a=>a.split`,`),s=d[0].map((a,i)=>d.reduce((b,c)=>(n=c[i].length)>b?n:b,0)),d=d.map(a=>`│${a.map((b,i)=>b.padEnd(s[i])).join`│`}│`),d.splice(1,0,(g=h=>h[0]+s.map(a=>'─'.repeat(a)).join(h[1])+h[2])('├┼┤')),g('┌┬┐')+`
${d.join`
`}
`+g('└┴┘'))

padEndFireFox固有のを使用します。


1
これは288バイトではありませんか?
アダム

1
@Adám...はい...修正
Mwr247

あなたはこのalotを使用しますが、(最後と後ろにバックティックg('└┴┘')を付けてg└┴┘)と同等ではありませんgか?
-NoOneIsHere

1
padEnd非標準です。必要な実行環境を指定する必要があります。
ニール

1
また、書く場所がいくつかあります。`foo`+bar+`baz`テンプレートを使用してバイトを保存できます`foo${bar}baz`
ニール

1

JavaScript(ES6)、281バイト

注:OPの要求に応じて、改行を含む単一の文字列として入力してください。他の答えは文字列リストを使用します-入力で文字列配列を使用すると、最初の分割を避けて9バイトをカットできます。

l=>(l=l.split`
`.map(r=>r.split`,`.map((w,i)=>(v=w.length)<c[i]?w:(c[i]=v,w)),c=[k=0]),l=l.map(r=>r.map((v,i)=>(v+' '.repeat(c[i]-v.length)))),[h=c.map(x=>'─'.repeat(x)),l.shift(),h,...l,h].map(a=>'│┌├└'[j=a!=h?0:++k]+a.join('│┬┼┴'[j])+'│┐┤┘'[j]).join`
`)

少ないゴルフ

l=>(
  // split input in an array of string arrays
  // meanwhile find the column widths and put them in *c*
  l = l.split`\n`.map(r=>r.split`,`.map((w,i)=>(v=w.length)<c[i]?w:(c[i]=v,w)),c=[]),

  // pad each column to the max column width
  l = l.map(r=>r.map((v,i)=>(v+' '.repeat(c[i]-v.length)))),

  // put in *h* the horizontal lines for top,bottom and head separator
  h = c.map(x => '─'.repeat(x) ),

  // add the *h* line at top, bottom and after head line
  l = [h, l.shift(), h, ...l, h],

  // rebuild a string, joining columns with '|' unless the row is *h*
  // if the row is *h* use different characters to join columns
  k = 0, 
  l.map(a=> '│┌├└'[j=a!=h?0:++k] + a.join('│┬┼┴'[j]) + '│┐┤┘'[j])
  .join`\n`  
)

テスト

F=
l=>(l=l.split`
`.map(r=>r.split`,`.map((w,i)=>(v=w.length)<c[i]?w:(c[i]=v,w)),c=[k=0]),l=l.map(r=>r.map((v,i)=>(v+' '.repeat(c[i]-v.length)))),[h=c.map(x=>'─'.repeat(x)),l.shift(),h,...l,h].map(a=>'│┌├└'[j=a!=h?0:++k]+a.join('│┬┼┴'[j])+'│┐┤┘'[j]).join`
`) 
  
function update() {
  O.textContent = F(I.value)
}
update()
#I { width:60%; height: 8em} 
<textarea id=I>Name,Age,Gender
Shaun,19,Male
Debra,19,Female
Alan,26,Male
George,15,Male</textarea><br>
<button onclick='update()'>Go</button>
<pre id=O></pre>


0

Python 3、318バイト

%フォーマットを使用する場合は-3バイト、短縮する場合は-1 バイトstr.join

L=[c.split(',')for c in input().split('\n')]
m=[max(len(x)for x in c)for c in zip(*L)]
L=[[""]+[d.ljust(n)for d,n in zip(c,m)]+[""]for c in L]
g=["─"*i for i in m]
J=str.join
print('\n'.join(["┌%s┐"%J("┬",g),J("│",L[0]),"├%s┤"%J("┼",g)]+[J("│",L[i])for i in range(1,len(L))]+["└%s┘"%J("┴",g)]))

引用符で囲まれた入力が必要です。


1
私には318バイトのように見えます。
アダム

1
@Adámあなたは正しいです、私は文字を見ました。
カールナップ

input()コールごとに1行しか使用しないため、機能しません。行がなくなるまで呼び出すinput()か、から直接読み取る必要がありstdinます。
movatica

それ以外:292バイト
movatica

0

C#、696バイト

ゴルフ:

string T(string[]f){int w=f.Max(r=>r.Length),a=f.Select(r=>r.Split(',')[0].Length).Max(),b=f.Select(r=>r.Split(',')[1].Length).Max(),c=f.Select(r=>r.Split(',')[2].Length).Max();string o="",n="\r\n",d="",j=string.Concat(Enumerable.Repeat("─",a)),k=string.Concat(Enumerable.Repeat("─",b)),l=string.Concat(Enumerable.Repeat("─",c));Func<string,int,string>z=(q,p)=>{return q.PadRight(p);};d="┌"+j+"┬"+k+"┬"+l+"┐";o+=d+n;var g=f.First().Split(',');o+="|"+z(g[0],a)+"|"+z(g[1],b)+"|"+z(g[2],c)+"|";d="├"+j+"┼"+k+"┼"+l+"┤";o+=n+d+n;for(int i=1;i<f.Length;i++){var h=f[i].Split(',');o+="|"+z(h[0],a)+"|"+z(h[1],b)+"|"+z(h[2],c)+"|"+n;}d="└"+j+"┴"+k+"┴"+l+"┘";o+=d;return o;}

Ungolfed(そしてより良い、^それは誰にも役に立たないので)

public string T(string[] c)
{
  int width = c.Max(r => r.Length),
    longestFirstColumn = c.Select(r => r.Split(',')[0].Length).Max(),
    longestSecondColumn = c.Select(r => r.Split(',')[1].Length).Max(),
    longestThirdColumn = c.Select(r => r.Split(',')[2].Length).Max();

  string o = "", lr = "\r\n", border = "",
    firstColumnFiller = string.Concat(Enumerable.Repeat("─", longestFirstColumn)),
    secondColumnFiller = string.Concat(Enumerable.Repeat("─", longestSecondColumn)),
    thirdColumnFiller = string.Concat(Enumerable.Repeat("─", longestThirdColumn));

  Func<string, int, string> padRight = (a, b) => { return a.PadRight(b); };

  border = "┌" + firstColumnFiller
    + "┬" +
    secondColumnFiller + "┬"
    + thirdColumnFiller
    + "┐";

  o += border + lr;

  var firstRow = c.First().Split(',');

  o += "|" + padRight(firstRow[0], longestFirstColumn) +
    "|" + padRight(firstRow[1], longestSecondColumn) +
    "|" + padRight(firstRow[2], longestThirdColumn) + "|";

  border = "├" +
    firstColumnFiller + "┼" +
    secondColumnFiller + "┼" +
    thirdColumnFiller
    + "┤";

  o += lr + border + lr;

  for (int i = 1; i < c.Length; i++)
  {
    var row = c[i].Split(',');

    o += "|" + padRight(row[0], longestFirstColumn) + "|"
    + padRight(row[1], longestSecondColumn) + "|" +
    padRight(row[2], longestThirdColumn) + "|" + lr;
  }

  border = "└" +
    firstColumnFiller + "┴" +
    secondColumnFiller + "┴" +
    thirdColumnFiller
    + "┘";

  o += border;

  return o;
}

テスト:

┌──────┬───┬──────┐         ┌──────────┬───────────────────────────┬─────┐
|Name  |Age|Gender|         |Name      |PPCG Challenge             |Votes|
├──────┼───┼──────┤         ├──────────┼───────────────────────────┼─────┤
|Shaun |19 |Male  |         |Pete Arden| Print all integers        | 4   |
|Debra |19 |Female|         |Pete Arden| Yes of course I'm an adult| 3   |
|Alan  |26 |Male  |         |Pete Arden| 5 Favorite Letters        | 1   |
|George|15 |Male  |         └──────────┴───────────────────────────┴─────┘
└──────┴───┴──────┘

どういうわけか、これを数えると697バイトを取得し続けます。
アダム

@Adámもう一度チェックすると、Visual StudioではGolfed文字列の長さは666列です。とにかく、666も697もどちらも正確な競争スコアではありません:)
ピートアーデン

末尾に改行がありますが、それを削除しても、696バイトです。
アダム

@AdámAh ...私は、文字数とバイト数の不一致が私をつまずかせるのを待っていました。この1つ( "funny")でこれらの面白い記号を知っている必要があります。更新、ありがとう:)
ピートアーデン

上記のコメントを参照Is input using list or array of strings (and no newlines) valid? Nope.
-edc65

0

Perl、273 + 9(-CS -nlaF,フラグ)= 282バイト

$v[$.-1]=[@F];map$l[$_]<($l=length$F[$_])&&($l[$_]=$l),0..$#F}sub p{printf$p,@_}sub o{p
pop,map{$\x$l[$_],$_-$#l?$_[0]:pop}0..$#l}$p=join'%s','',(map"\%-${_}s",@l),$/;($\,$c,@c)=map
chr$_*4+9472,0,.5,3..15;o@c[8,1,0];p($c,map{$_,$c}@$_),$i++||o@c[12,6,4]for@v;o@c[10,3,2];{

を使用して:

cat file.csv | perl -CS -nlaF, script.pl

Ideoneで試してみてください。


0

PHP、313バイト

for(;$r=fgetcsv(STDIN);$a[]=$r)foreach($r as$x=>$s)$e[$x]=max($e[$x],strlen($s));$t=["┬","┌","┐"];eval($L='foreach($e as$i=>$n)echo$t[!$i],str_repeat("─",$n);echo"$t[2]\n";');foreach($a as$k=>$r){foreach($r as$i=>$s)echo"│",str_pad($s,$e[$i]);echo"│\n";$t=["┼","├","┤"];if(!$k)eval($L);}$t=["┴","└","┘"];eval($L);

壊す

for(;$r=fgetcsv(STDIN);$a[]=$r)                         // read csv from STDIN, append to array $a
    foreach($r as$x=>$s)$e[$x]=max($e[$x],strlen($s));  // remember max length in array $e
                                                        // print top border
$t=["┬","┌","┐"];eval($L='foreach($e as$i=>$n)echo$t[!$i],str_repeat("─",$n);echo"$t[2]\n";');
foreach($a as$k=>$r)
{
    foreach($r as$i=>$s)echo"│",str_pad($s,$e[$i]);echo"│\n";   // print row
    $t=["┼","├","┤"];if(!$k)eval($L);                           // print border below header
}
$t=["┴","└","┘"];eval($L);                              // print bottom border

ideoneでテストする


0

APL(Dyalog Extended)36 25 バイトSBCS

完全なプログラム。それABCDEFGHIJKLMNOPQRSTUVWXYZがCSVファイルであると仮定します。標準出力に出力します。

disp(1m)⍪↑¨↓⍉1m←⎕CSVA

オンラインでお試しください!

⎕A 大文字 lphabet(参照が最も短い組み込み文字列)
⎕CSV はそのファイルを読み取り、CSVから行列
m← ストアに変換しますmm atrixの場合)
1↓ 最初の行を
 転置
 し、列のリストに転置
↑¨ し、各文字列のリストを行列にミックスします
(…その)⍪ 上に以下を積み重ねます:それ
1↑m にm
⌂disp 適用dfns.dispする最初の行を取ります(線画文字を描きます)

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.