Is it possible to reset a new-line character when read txt file in julia?

question

#1

In perl, if you want reset new-line character, you can use
$/="new_character". So is it possible to reset a new-line character when read txt file in julia ?

Thanks~
Regard
Si


#2

I don’t understand what this means.

If you want to remove trailing newlines from a string, use chomp.


#3

I don’t think that you can set a custom separator in a global way like you do in Perl. However, what you can do is read your file using readuntil instead of readline if you want to be able to specify a delimiter for the chunk that you want to read.

Here is an example taken from the documentation of readuntil, where the character L is used as a delimiter.

  julia> open("my_file.txt", "w") do io
             write(io, "JuliaLang is a GitHub organization.\nIt has many members.\n");
         end
  57
  
  julia> readuntil("my_file.txt", 'L')
  "Julia"

#4

sorry, I should use more accurate word like delimiter as @ffevotte


#5

thanks~ I should use delimiter. because don’t know the how many lines by new delimiter, so how to loop the all lines by new delimiter?


#6

I use this to loop

open("tmp") do file
	while ! eof(file)
		line = readuntil(file, ">")
		line = chomp(line)
		println("line is ",line)
	end
end

#7

Something like that, I don’t think you need chomp unless there is a newline to remove. Also, I would specify readuntil(...; keep = false) to be explicit, although that is the default.