Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

211
Views
Cómo leer un archivo en Julia como Python

¿Por qué hay una diferencia entre estos?:

 # Python f = open("./text.txt", "r") for i in f.readlines(): for l in i: print(print(l == "\n", ":", l)) f.close() # ----------------------------- # Julia f = open("./text.txt", "r") while !eof(f) for l in readline(file) println(l == '\n', " : ", l) end end close(f)

El de Python genera esto:

 False : h False : e False : l False : l False : o False : False : W False : o False : r False : l False : d True : <--- yep, it is as expected False : y False : a False : y

El de Julia genera esto:

 false : h false : e false : l false : l false : o false : <--- is this not a \n?? false : W false : o false : r false : l false : d false : y false : a false : y

text.txt es este:

 hello World yay

Como puede ver, las salidas son diferentes. ¿Cómo puedo hacer que el de Julia se comporte como el de Python? ¿Hay otras formas de leer un archivo en Julia?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

si le gusta leer un carácter a la vez, debe usar readeach , por ejemplo

 f = open("./text.txt", "r") for l in readeach(f, Char) println(l == '\n', " : ", l) end false : h false : e false : l false : l false : o false : false : w false : o false : r false : l false : d true : false : y false : a false : y

Por cierto, solo tenga en cuenta que esta no es una forma eficiente.

over 4 years ago · Santiago Trujillo Report

0

Puedes hacerlo:

 # Julia f = open("./text.txt", "r") while !eof(f) for l in readline(file, keep=true) println(l == '\n', " : ", l) end end close(f)

De forma predeterminada, descarta \n , pero puede conservarlos agregando keep=true .

over 4 years ago · Santiago Trujillo Report

0

Aquí hay una versión más juliana (y corregida) usando la sugerencia keep=true del usuario 17558100:

 open("./text.txt", "r") do f for l in readlines(f, keep=true) foreach(c->println(c == '\n', " : ",c), l) end end

Este formato do es el mismo que el with de Python y esto asegura que el archivo se cierre sin una llamada explícita a close() .

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!