2

I have a csv file with random data that I can import into a table but is it possible to create columns for that table while I import the csv

I tried this but it doesn't seem to work it gets a type error:

details:`time`place`cost`total`address ("TSFIS";",") 0:`data.csv
user3043724
  • 51
  • 1
  • 6

1 Answers1

6

You're not far off - you need to covert the list of lists you get back from 0: into a dictionary and flip it to create a table:

detailsTbl:flip`time`place`cost`total`address!("TSFIS";",") 0:`data.csv

Example:

/ start off by writing some data
q)hclose hopen[`:data.csv] "a,b,c,d\naa,bb,cc,dd\naaa,bbb,ccc,ddd"
q)read0`:data.csv
    "a,b,c,d"
    "aa,bb,cc,dd"
    "aaa,bbb,ccc,ddd"

/ lets inspect what we get back from 0:
q)show data:("SSSS";",")0:`:data.csv
    a aa aaa
    b bb bbb
    c cc ccc
    d dd ddd

/ convert to dict + flip to create table
q)flip `aCol`bCol`cCol`dCol!data
    aCol bCol cCol dCol
    -------------------
    a    b    c    d
    aa   bb   cc   dd
    aaa  bbb  ccc  ddd
MdSalih
  • 1,978
  • 10
  • 16