cancel
Showing results for 
Search instead for 
Did you mean: 

Backslash (\) Delimited TXT Files and Reading Bytes???

planefan
Contributor

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? 

8 REPLIES 8

dcrossey
New Contributor III
New Contributor III

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

David

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! 

SJT
Valued Contributor
Valued Contributor

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

sstewart97
New Contributor II

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. 

Thanks SStewart! What do I do if the file reads in singular backslash? 

"000\000\000\000"

 

sstewart97
New Contributor II

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?

planefan
Contributor

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. 

sstewart97
New Contributor II

Hi ticktick, could you provide the whole line? Thanks, Stephen.