Python style question around reading small files

What is the most pythonic way of reading in a named file, lines of lines that are either empty, contain only spaces, or have # as the first character, and then process the remaining lines? Suppose all of this fits easily into memory.

Note: this is not difficult to do - what I ask is the most pythonic way. I wrote a lot of Ruby and Java and have lost the feeling.

Here's the straw:

file_lines = [line.strip() for line in open(config_file, 'r').readlines() if len(line.strip()) > 0]
for line in file_lines:
  if line[0] == '#':
    continue
  # Do whatever with line here.

I'm interested in nodule, but not at the cost of reading difficulties.

+3
source share
8 answers
lines = [r for r in open(thefile) if not r.isspace() and r[0] != '#']

.isspace(), , , - , len(r.strip()) == 0 (ech; -).

+3

. , .

def RemoveComments(lines):
    for line in lines:
        if not line.strip().startswith('#'):
            yield line

def RemoveBlankLines(lines):
    for line in lines:
        if line.strip():
            yield line

:

filehandle = open('myfile', 'r')
for line in RemoveComments(RemoveBlankLines(filehandle)):
    Process(line)

, , , .

+5
for line in open("file"):
    sline=line.strip()
    if sline and not sline[0]=="#" :
       print line.strip()

$ cat file
one
#
  #

two

three
$ ./python.py
one
two
three
+2

:

processed = [process(line.strip())
             for line in open(config_file, 'r')
             if line.strip() and not line.strip().startswith('#')]

, , - . :

processed = [process(line)
             for line in (line.strip() for line in open(config_file, 'r'))
             if line and not line.startswith('#')]
+1

, ..

, , # ,

, , , .

with open("config_file","r") as fp:
    data = (line for line in fp if line.strip() and not line.startswith("#"))
    for item in data:
        print repr(item)
+1

, -:

from itertools import ifilter, ifilterfalse, imap

with open(r'c:\temp\testfile.txt', 'rb') as f:
    s1 = ifilterfalse(str.isspace, f)
    s2 = ifilter(lambda x: not x.startswith('#'), s1)
    s3 = imap(str.rstrip, s2)
    print "\n".join(s3)

, , , , , , . iscomment .

+1

, . , :

fp = open('file.txt')
for line in fp:
    line = line.strip()
    if line and not line.startswith('#'):
        # process
fp.close()

, .

0

Using slightly newer idioms (or with Python 2.5 from __future__ import with), you can do this, which has the advantage of safe cleanup, but rather concise.

with file('file.txt') as fp:
    for line in fp:
        line = line.strip()
        if not line or line[0] == '#':
            continue

        # rest of processing here

Note that deleting the first line means that checking for "#" will actually reject the lines with this as the first is not empty, and not just "like the first character". Easy enough to change if you strictly adhere to this.

0
source

Source: https://habr.com/ru/post/1730969/


All Articles