Does reading form io.stdin work?
lateefj opened this issue · 9 comments
lateefj commented
I am trying to read a csv file from stdin:
for fields in csv.open(io.stdin) do
for i, v in ipairs(fields) do print(i, v) end
end
I keep getting this error:
lua: /home/lhj/local/share/lua/5.2/csv.lua:340: bad argument #1 to 'open' (string expected, got userdata)
stack traceback:
[C]: in function 'open'
/home/lhj/local/share/lua/5.2/csv.lua:340: in function 'open'
user_comment_size/lua/comment_count.lua:6: in main chunk
[C]: in ?
Would love any feedback?
geoffleyland commented
Try csv.use(io.stdin)
or maybe just csv.use()
, I think?
lateefj commented
Both seem to give me this error (lua 5.2 and luajit):
lua: user_comment_size/lua/comment_count.lua:6: attempt to call a table value
stack traceback:
user_comment_size/lua/comment_count.lua:6: in main chunk
[C]: in ?
geoffleyland commented
You're forgetting a :lines()
lateefj commented
local csv = require("csv")
local count = 0
local size = 0
for fields in csv.use(io.stdin:lines()) do
--for line in io.stdin:lines() do
for i, v in ipairs(fields) do
size = size + 1
print(i, v)
end
count = count + 1
--io.write("count " , count, " size ", size, "\r")
end
io.write("count " , count, " size ", size, "\n")
geoffleyland commented
Try
for fields in csv.use(io.stdin):lines() do
lateefj commented
Error:
lua: user_comment_size/lua/comment_count.lua:6: /usr/local/share/lua/5.2/csv.lua:150: attempt to index local 'parameters' (a nil value)
stack traceback:
[C]: in function 'for iterator'
user_comment_size/lua/comment_count.lua:6: in main chun
lateefj commented
Worked like this:
for fields in csv.use(io.stdin, {}):lines() do
lateefj commented
Thanks a lot!
geoffleyland commented
Weird, you shouldn't need to do that, and you shouldn't have got an error at line 150. This line is there so that parameters is optional. Oh well...