Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate over the lines in a string?

Tags:

julia

I have a long string in Julia. I'd like to apply some operation to each line. How can I efficiently iterate over each line? I think I can use split but I am wondering if there is a method that won't allocate all the strings upfront?

like image 516
DVNold Avatar asked Nov 02 '20 15:11

DVNold


People also ask

How do you iterate over a line in Python?

Use a for-loop to iterate through the lines of a file In a with-statement, use open(file, mode) with mode as "r" to open file for reading. Inside the with-statement, use a for-loop to iterate through the lines. Then, call str. strip() to strip the end-line break from each line.

Can you iterate through strings in Python?

You can traverse a string as a substring by using the Python slice operator ([]). It cuts off a substring from the original string and thus allows to iterate over it partially. To use this method, provide the starting and ending indices along with a step value and then traverse the string.


1 Answers

You can use eachline for this:

julia> str = """
       a
       b
       c
       """
"a\nb\nc\n"

julia> for line in eachline(IOBuffer(str))
         println(line)
       end
a
b
c

There's also a version that operates directly on a file, in case that's relevant to you:

help?> eachline
search: eachline eachslice

  eachline(io::IO=stdin; keep::Bool=false)
  eachline(filename::AbstractString; keep::Bool=false)

  Create an iterable EachLine object that will yield each line from an I/O stream or a file. Iteration calls readline on
  the stream argument repeatedly with keep passed through, determining whether trailing end-of-line characters are
  retained. When called with a file name, the file is opened once at the beginning of iteration and closed at the end. If
  iteration is interrupted, the file will be closed when the EachLine object is garbage collected.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> open("my_file.txt", "w") do io
             write(io, "JuliaLang is a GitHub organization.\n It has many members.\n");
         end;
  
  julia> for line in eachline("my_file.txt")
             print(line)
         end
  JuliaLang is a GitHub organization. It has many members.
  
  julia> rm("my_file.txt");

If you already have the complete string in memory then you can (and should) use split, as pointed out in the comments. split basically indexes into the string and doesn't allocate new Strings for each line, as opposed to eachline.

like image 122
pfitzseb Avatar answered Oct 09 '22 03:10

pfitzseb