9
\$\begingroup\$

Input a CSV table, like this:

data00,data01,data02,...,data0m
data10,data11,data12,...,data1m
...
datan0,datan1,datan2,...,datanm

The cells in the CSV table is guaranteed to contain only letters or numbers, and there’s no extra whitespace.

Your task is to convert it to a MediaWiki table, and output it, like this:

{|
| data00 || data01 || data02 || ... || data0m
|-
| data10 || data11 || data12 || ... || data1m
|-
...
|-
| datan0 || datan1 || datan2 || ... || datanm
|}

Your output must strictly follow this format, except for the whitespaces before or after each cell.

For example, input:

a,b,c
1,2,3

Will output:

{|
| a || b || c
|-
| 1 || 2 || 3
|}

but

{|
| a || b || c
|-
|1||2||3
|}

is also a valid output. This is , so use the fewest bytes.

\$\endgroup\$
0

14 Answers 14

3
\$\begingroup\$

Rust, 73 bytes 68 bytes

|s|format!("{{|
|")+&s.replace(",","||").replace("
","
|-
|")+"
|}";

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ 65 bytes: |s|format!("{{| ")+&s.replace(",","||").replace(" "," |- ")+" |}" \$\endgroup\$
    – corvus_192
    Commented May 24 at 13:52
  • \$\begingroup\$ (stack exchange ate the newlines, sorry about that) \$\endgroup\$
    – corvus_192
    Commented May 24 at 15:15
  • 1
    \$\begingroup\$ This is missing the | at the start of each line of cells. \$\endgroup\$
    – bigyihsuan
    Commented May 24 at 18:17
  • \$\begingroup\$ @corvus_192 That gives me an error saying that type annotations are needed for "s". Once I set it up and fix the missing | (as pointed out by @bigyihsuan), it's 73 bytes... maybe I'm missing something? Here's the link: Try it online! \$\endgroup\$ Commented May 24 at 20:20
  • 1
    \$\begingroup\$ Just put a type sugnature after let f: TIO \$\endgroup\$
    – corvus_192
    Commented May 25 at 9:18
2
\$\begingroup\$

Retina, 25 bytes

,
||
¶
¶|-¶|
^
{|¶|
$
¶|}

Attempt This Online!

\$\endgroup\$
2
\$\begingroup\$

Perl 5 -p0, 35 bytes

s/
/
|-
|/g;s/,/||/g;$_="{|
|$_
|}"

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Go, 102 bytes

import."strings"
func f(s string)string{R:=ReplaceAll
return`{|
|`+R(R(s,",","||"),`
`,`
|-
|`)+`
|}`}

Attempt This Online!

\$\endgroup\$
2
\$\begingroup\$

sed, 31 bytes

1i{|
1!i|-
s/^/|/
s/,/||/g
$a|}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 51 bytes

s=>`{|
|${s.replace(/\W/g,c=>1/c?`
|-
|`:`||`)}
|}`

Try it online!

Commented

with funny colors, courtesy of highlight.js

s =>              // s = input string
`{|\n|${          // append the leading string "{|\n|"
  s.replace(      // replace in s:
    /\W/g,        //   because the data fields are guaranteed
                  //   to contain only letters and digits, it
                  //   is safe to use \W to match all commas
                  //   and newline characters at once
    c =>          //   given the matched character c ...
      1 / c ?     //     if c = "\n" (which is zero'ish):
        `\n|-\n|` //       append the row separator
      :           //     else:
        `||`      //       append the column separator
  )               //   end of replace()
}\n|}`            // append the trailing string "\n|}"
\$\endgroup\$
1
\$\begingroup\$

Google Sheets, 89 84 bytes

-5 bytes thanks to @doubleunary

="{|
"&REGEXREPLACE(SUBSTITUTE(SUBSTITUTE(A1,",","||"),"
","
-
"),"(?m)^","|")&"
|}"

enter image description here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can save a byte by using hardcoded newlines instead of an array: ="{|↲"&regexreplace(regexreplace(substitute(A1,",","||"),"↲","$0-$0"),"(?m)^","|")&"↲|}". That also gets all of the result in one cell. Press Control+Enter to get newlines in place of . \$\endgroup\$ Commented May 24 at 17:11
  • 1
    \$\begingroup\$ @doubleunary, nice! this helped me save 5 bytes by also using in place of $0 which saved 2 bytes and allowed me to SUBSTITUTE instead of REGEXREPLACE saving 2 more bytes. \$\endgroup\$
    – z..
    Commented May 24 at 19:03
1
\$\begingroup\$

Python 3, 65 bytes 59 bytes

  • Removed 6 bytes thanks to corvus_192
  • Fixed an error as pointed out by bigyihsuan on my other answer where I forgot the | in front of each line of data
lambda s:"{|\n|"+s.translate({44:"||",10:"\n|-\n|"})+"\n|}"

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 57 bytes: lambda s:"{|\n"+s.translate({44:"||",10:"\n|-\n"})+"\n|}" \$\endgroup\$
    – corvus_192
    Commented May 24 at 14:52
1
\$\begingroup\$

Vyxal, 149 bitsv2, 18.625 bytes

⌐ƛ\|dj\-M;fṪ¤ø.\|vp⁋øḃ

Try it Online!

Bitstring:

00011000001001100101010010010000100010000101001011101000010010010110110010001010110101010011111010101100011110000111011001000010001001111011110011110

-1.5 bytes from doing the dash separator inside the map function

⌐ƛ\|dj;:L‹\-ẋY¤ø.\|vp⁋øḃ­⁡​‎‎⁡⁠⁡‏⁠‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏⁠‎⁡⁠⁢⁣‏‏​⁡⁠⁡‌⁣​‎⁠⁠⁠⁠‎⁡⁠⁢⁢‏⁠‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁢⁡​‎⁠⁠‎⁡⁠⁤⁡‏⁠‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌⁢⁤​‎‎⁡⁠⁤⁢‏‏​⁡⁠⁡‌⁣⁡​‎‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏⁠‎⁡⁠⁢⁡⁡‏‏​⁡⁠⁡‌⁣⁢​‎‎⁡⁠⁢⁡⁢‏⁠‎⁡⁠⁢⁡⁣‏⁠‎⁡⁠⁢⁡⁤‏⁠‎⁡⁠⁢⁢⁡‏⁠‏​⁡⁠⁡‌⁣⁣​‎‎⁡⁠⁢⁢⁢‏⁠‎⁡⁠⁢⁢⁣‏⁠‎⁡⁠⁢⁢⁤‏‏​⁡⁠⁡‌­
⌐                         # ‎⁡split strings on commas
 ƛ    ;                   # ‎⁢for each row
     j                    # ‎⁣join on the string
  \|d                     # ‎⁤"||"
            ẋ             # ‎⁢⁡repeat the string...
          \-              # ‎⁢⁢"-"
       :L‹                # ‎⁢⁣rows-1 times 
             Y            # ‎⁢⁤interleave with separated list
              ¤ø.         # ‎⁣⁡surround with empty string
                 \|vp     # ‎⁣⁢prepend a \| to each row
                     ⁋øḃ  # ‎⁣⁣join list on newlines and add curly braces to ends
💎

Created with the help of Luminespire.

\$\endgroup\$
1
\$\begingroup\$

Charcoal, 20 bytes

{|¶WS⁺|⟦⪫⪪ι,×|²-⟧M↗}

Try it online! Link is to verbose version of code. Takes input as a list of newline-terminated strings. Explanation:

{|¶

Output the table start.

WS

Loop over each row.

⁺|⟦⪫⪪ι,×|²-⟧

Replace the commas in the row with ||, then print both the row and a -, but prefixing a | to each. (×|² is shorter than || because it doesn't need to be separated from the adjacent strings.)

M↗}

Change the last table row into a table end.

17 bytes to print using the alternative cell format:

{|¶WS⁺|⊞O⪪ι,¦-M↗}

Try it online! Link is to verbose version of code. Takes input as a list of newline-terminated strings. Explanation: As above except each row is split on commas and a - is appended to the list before each element is prefixed with |.

\$\endgroup\$
0
\$\begingroup\$

JavaScript (Node.js), 58 bytes

s=>`{|
|${s.replace(/,/g,'||').replace(/\n/g,`
|-
|`)}
|}`

Attempt This Online!

\$\endgroup\$
0
\$\begingroup\$

AWK, 75 bytes

BEGIN{x="{|\n"}{gsub(/,/,"||");x=x (1~NR?"|":"-\n|")$0"\n|"}END{print x"}"}

Try it online!

AWK is pretty clear about concatenating strings, so we're just jamming in the extra formatting. Run it thus, with the csv file of choice:

awk 'BEGIN{x="{|\n"}{gsub(/,/,"||");x=x (1~NR?"|":"-\n|")$0"\n|"}END{print x"}"}' data.csv
\$\endgroup\$
1
  • \$\begingroup\$ 66 bytes, using -F, -vOFS=||: BEGIN{printf"{|\n|"}NR~2{s="|-\n|"}{$1=$1;print s$0}END{print"|}"} tio.run/##SyzP/v/… \$\endgroup\$
    – corvus_192
    Commented May 25 at 10:06
0
\$\begingroup\$

APL+WIN, 61 bytes.

Prompts for CSV as a nested matrix of rows:

m←⎕⋄((∊,m=',')/∊,m)←⊂'||'⋄⊃(⊂'{|'),(¯1↓,('|',¨m),⊂'|-'),⊂'|}'

Try it online! Thanks to Dyalog Classic

\$\endgroup\$
0
\$\begingroup\$

05AB1E, 22 (or 20) bytes

|',„||:'-.ý'}ª'|ì„{|š»

Input-lines as separated inputs.

Try it online.

: If we're allowed to use a list of lines as I/O, it could be -2 bytes by removing the leading | and trailing »:
Try it online.

Explanation:

|                       # Get all inputs as a list of strings
 ',„||:                '# Replace all "," with "||" in each string
       '-.ý            '# Intersperse it with "-" delimiter
           '}ª         '# Append "}" to the list
              '|ì      '# Prepend "|" in front of each inner string
                 „{|š   # Prepend "{|" to the list
                     »  # Join the list with newline delimiter
                        # (after which the result is output implicitly)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.