Quoting strings without quotes when using writeetable () with DataFrames.jl in Julia?

The default for writing rows in DataFrames to a file using writeetable is that they are surrounded by quotation marks:

using DataFrames
df = DataFrame(letters=["A","B","C"],numbers=[1,2,3])
writetable("df_file", df, separator='\t')

displays the following file:

"letters"   "numbers"
"A" 1
"B" 2
"C" 3

It is possible to change the quotemark symbol:

writetable("df_file", df, separator='\t', quotemark='.')

.letters.   .numbers.
.A. 1
.B. 2
.C. 3

but this does not work if no character is specified

writetable("df_file", df, separator='\t', quotemark='')
ERROR: syntax: invalid character literal

My question is: how can I write strings without quotes at all? This will be the result I need:

letters numbers
A   1
B   2
C   3

I am currently using version Julia 0.4.1, DataFrames package version 0.6.10.

+4
source share
2 answers

GitHub DataFrames , .

, Array, Julia writedlm(), , :

writedlm(FileName, convert(Array,df), '\t')

, - :

open(FileName, "w") do f
    writedlm(f, names(df)', '\t')
    writedlm(f, convert(Array,df), '\t')
end

. : writetable() - Julia

+3

:

julia> n, p = size(df)
(3,2)

julia> open("/tmp/df_file.txt", "w") do f
            for i in 1:n
               for j in 1:p
                  write(f, string(df[i, j]))
                  write(f, "\t")
               end
               write(f, "\n")
             end
        end

, - ( writetable):

julia> function myprinttable(io::IO,
                           df::AbstractDataFrame;
                           header::Bool = true,
                           separator::Char = ',',
                           quotemark::AbstractString = "\"",
                           nastring::AbstractString = "NA")
           n, p = size(df)
           etypes = eltypes(df)
           if header
               cnames = DataFrames._names(df)
               for j in 1:p
                   print(io, quotemark)
                   print(io, cnames[j])
                   print(io, quotemark)
                   if j < p
                       print(io, separator)
                   else
                       print(io, '\n')
                   end
               end
           end
           quotestr = quotemark
           for i in 1:n
               for j in 1:p
                   if ! (isna(df[j],i))
                       if ! (etypes[j] <: Real)
                           print(io, quotemark)
                           DataFrames.escapedprint(io, df[i, j], quotestr)
                           print(io, quotemark)
                       else
                           print(io, df[i, j])
                       end
                   else
                       print(io, nastring)
                   end
                   if j < p
                       print(io, separator)
                   else
                       print(io, '\n')
                   end
               end
           end
           return
       end
julia> open("/tmp/df_file.txt", "w") do f
          myprinttable(f, df, header=true, separator='\t', quotemark="")
       end

( .)

quotemark Char String. ( Char .

+3

Source: https://habr.com/ru/post/1649999/


All Articles