CSV.jl, Write to files

I was trying wite the companies name to CSV files. But I am getting the values
what is the error in this code

Letters=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
url = "https://www.moneycontrol.com/india/stockpricequote/"
global CompanyList=Array{String,1}()


    for Letter in Letters

        response = HTTP.get(url*Letter)
        html = response.body |> String |> parsehtml
        elements = eachmatch(sel".bl_12", html.root)
        #global CompanyList=Array{String,1}()
        for ele in Array{Int,1}(range(3,length(elements)))

            try
                #println(ele,"========", elements[ele][1].text)
                Name=elements[ele].attributes["href"]
                println(Name)
                df=DataFrame(Companies=Name)
                CSV.write("C:\\Users\\Office\\Documents\\Code\\Company.csv",df)
                #elements[3][1].text




            catch  err
                if isa(err,BoundsError)
                    break
                end
                #for com in range(1:length(CompanyList))
                    #println(com)
                #end
            end
        end
    end

ERROR: ArgumentError: ‘String’ iterates ‘Char’ values, which don’t satisfy the Tables.jl Row-iterator interface

Change the complex:

for ele in Array{Int,1}(range(3,length(elements)))

to

for ele in 3:length(elements)

Also, the catch of the error avoid to receive any important error, please, do not do that.

The rest of the source has not much sense, you are creating a DataFrame with only a string, that is the error. Something like that could be more useful:

    for Letter in Letters
        response = HTTP.get(url*Letter)
        html = response.body |> String |> parsehtml
        elements = eachmatch(sel".bl_12", html.root)
        names = String[]
        # global CompanyList=Array{String,1}()
        for ele in 3:length(elements)
            try
                push!(names, elements[ele].attributes["href"])
            catch  err
                println(err)
            end
        end

        df=DataFrame(Companies=names)
        CSV.write("Company.csv",df)
end

In that case the file is still always been rewritten, but at least it is working, because you are creating the DataFrame using an array.

1 Like

Could you please explain the mistakes in my code?
Array can not be pushed to CSV?

Your variable Name was an String, and to create a DataFrame you have to give an Iterator (as an Array), not a String. So I have created an array, “names”, push all names in it, and at the end I create the DataFrame using the Array as parameter.