2021.07.18 01:13 PM - edited 2021.07.18 01:15 PM
How does one deal with a \ delimited txt file when using \ as the delim breaks in a typical csv load?
Also, how does one deal with a no delimited txt file? In other words reading bytes?
Anyone know?
2021.07.19 03:19 AM
Hi ticktick,
Using an example file:
This\is\a\backslash\delimited\file
And\this\is\the\second\line
We can load it in using read0 as follows:
q)ssr[;"\\";" "] each read0 `:backslash.txt
"This is a backslash delimited file"
"And this is the second line"
To read bytes, use read1.
Further information on handling text data can be found on code.kx at:
Hope this helps!
David
2021.07.19 07:50 AM
Hey David, thanks! Unfortunately that didn't split the backslashes for me.
I guess to be more specific, the file is columns of data smushed together with a backslash to delimit them. I'm trying to get this all into a table.
Is there no way to use a "\\" in a normal csv call to accomplish this?
something like:
tab: ("sfff"; enlist "\\") 0: `path
Thanks!
2021.07.28 10:16 AM
Here is a text file bar.csv that fits your field descriptors.
name\c1\c2\c3
tom\1.1\1.2\1.3
duck\2.1\2.2\2.3
harry\3.1\3.2\3.3
The backslash is a special character used to escape special characters. To write one, escape it.
q)count"\\"
1
Field descriptors are upper case, as in the left argument to Tok. Now your (slightly modified) Load CSV works:
q)("SFFF";enlist"\\")0:`bar.csv
name c1 c2 c3
-----------------
tom 1.1 1.2 1.3
duck 2.1 2.2 2.3
harry 3.1 3.2 3.3
2021.07.19 08:57 AM
I have made the following text file:
user@host:~$ vim text.txt
first\last\age
simon\says\40
michael\jordan\20
peter\pan\65
and when I read it in a q session it reads as:
q)read0 `:text.txt
"first\\last\\age"
"simon\\says\\40"
"michael\\jordan\\20"
"peter\\pan\\65"
(read0 used to read chars) so in order to read it into a table, I will need to pass "\\" as the delimiter, i.e.
q)show t:("SSI";enlist "\\")0:`:text.txt
first last age
------------------
simon says 40
michael jordan 20
peter pan 65
(0: used to read/write chars). See more about reading & writing files here at the kx docs.
2021.07.19 09:26 AM
Thanks SStewart! What do I do if the file reads in singular backslash?
"000\000\000\000"
2021.07.20 01:59 AM
When I create a text file with delimeter "\" and vim into it the backslash is replaced with caret notation null-type (^@), i.e.
q)`:astxt.txt 0:enlist "000\000\000\000"
q)\\
user@host$vim astxt.txt
000^@^@^@
so I think the "\000" is q interpreting the null type. Can you maybe provide more of the string?
2021.07.19 09:33 AM
Longstory short I have a dump file that I converted to txt file. When I read it into kdb its all 1 line and you can tell the columns are split by single backslash.
Thusfar all my attempts at opening/tabling this pig have failed. I apologize for the profanity.
2021.07.20 02:00 AM
Hi ticktick, could you provide the whole line? Thanks, Stephen.
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.