Python Forum
Improving the program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Improving the program
#5
Well, let's start at the top...

(May-13-2019, 04:50 PM)sunhyunshine Wrote:
def atidarymas(): #funkcija, atidaromas duomenu failas, panaikinami taskai, kableiai
    failas = open("duomenys.txt", "r")
    duomenys = failas.read()
    duomenys = duomenys.replace("\n", "")
    duomenys = duomenys.replace(".", "")
    duomenys = duomenys.replace(",", "")
    sar = duomenys.split()
    failas.close()
    return (sar)
1) with blocks are a good habit to get into, so the resource gets closed automatically when you're done with it (so you don't have to worry about forgetting to clean up after yourself).
2) Multiple replace operations might look better (?) as a regex, since it'd only be one line, and one operation.

import re

def atidarymas(): #funkcija, atidaromas duomenu failas, panaikinami taskai, kableiai
    with open("duomenys.txt", "r") as failas:
        duomenys = failas.read()
        sar = re.sub(r"[\n.,]", "", duomenyx).split()
        return sar
Reply


Messages In This Thread
Improving the program - by sunhyunshine - May-13-2019, 04:50 PM
RE: Improving the program - by heiner55 - May-17-2019, 03:45 PM
RE: Improving the program - by Larz60+ - May-17-2019, 08:43 PM
RE: Improving the program - by ichabod801 - May-17-2019, 09:55 PM
RE: Improving the program - by nilamo - May-17-2019, 09:58 PM
RE: Improving the program - by perfringo - May-18-2019, 05:31 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020