Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add writerow to write CSV format row-wise in a loop #1001

Closed
sairus7 opened this issue Apr 29, 2022 · 0 comments
Closed

Add writerow to write CSV format row-wise in a loop #1001

sairus7 opened this issue Apr 29, 2022 · 0 comments

Comments

@sairus7
Copy link

sairus7 commented Apr 29, 2022

If I want to write CSV file row by row, I should wrap each row with 1-element vector (Table-compatible source) and pass into CSV.write function. Maybe there can be a simpler version of CSV.writerow function, that can take any "row-compatible" object, like struct, tuple, named tuple, and so on?

It can be needed to write CSV-formatted logs, like in this MWE:

using CSV
using HTTP
using MsgPack

struct MyMsg
    name::String
    index::Int
    number::Float64
end
MsgPack.msgpack_type(::Type{MyMsg}) = MsgPack.StructType()

@async HTTP.WebSockets.listen("127.0.0.1", UInt16(1234)) do ws

    append = false
    while !eof(ws) # even with this line there is `EOFError: read end of file``
        bytes = readavailable(ws)
        msg = unpack(bytes, MyMsg)
        println(msg)

        #I want to save msg line-by-line into CSV without storing it into a vector of total number of messages
        CSV.write("test2.csv", [msg];, delim = '\t', append = append)
        append = true
    end
end

HTTP.WebSockets.open("ws://127.0.0.1:1234/"; binary=true) do ws
    for k = 1:10
        msg = MyMsg("hello", k, randn())
        write(ws, pack(msg))
    end
end
quinnj added a commit that referenced this issue May 5, 2022
Implements #1001. We already had the internal methods here, so this PR just adds some higher-level user-facing methods that take a plain "row" (from the Tables.jl "Row" interface) or an `io` and row. This is convenient when you don't have a traditional iterator (and can use RowWriter), but just want to repeatedly call `CSV.writerow` yourself and get the delimited output.
quinnj added a commit that referenced this issue May 6, 2022
Implements #1001. We already had the internal methods here, so this PR just adds some higher-level user-facing methods that take a plain "row" (from the Tables.jl "Row" interface) or an `io` and row. This is convenient when you don't have a traditional iterator (and can use RowWriter), but just want to repeatedly call `CSV.writerow` yourself and get the delimited output.
@quinnj quinnj closed this as completed May 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants