-2

我有一个像这样的文件:

<space>
<space>
line1
<space>
column 1    column 2    column 3   ...

.
.
.


<space>
<space>

如何删除这个多余的空格?

我需要提取将打开的标题line1。另外,我需要提取column 1,column 2column 3

在最后一栏内容的末尾有'\n'。如何摆脱它?

帮我解决这个...

谢谢

4

3 回答 3

4

首先打开文件并读取所有行:

f = open('filename string');
lines = f.readlines()

然后...

# remove empty lines
lines = [l for l in lines if len(l.strip()) > 0]
header = lines[0]
line = lines[1].split(' ')
column1 = line[0]
column2 = line[1]
...

还:

total_lines = len(lines)
total_columns = len(line)
于 2009-01-30T09:18:37.473 回答
1

一个简单的解决方案,strip()用于删除空格和split()分隔列数据:

>>> mylines
[' \n', ' \n', 'line1\n', ' \n', ' \n', 'column1    column2    column3 \n']
>>> def parser(lines):
...     header=""
...     data=[]
...     for x in lines:
...         line = x.strip()
...         if line == "":
...             continue
...         if header == "":
...             header=line
...         else:
...             data.append(line.split())
...     return {"header":header,"data":data}
... 
>>> parser(mylines)
{'header': 'line1', 'data': [['column1', 'column2', 'column3']]}
>>> 
于 2009-01-30T09:43:12.963 回答
0

使用生成器函数处理解析的每个元素

def nonEmptyLines( aFile ):
    """Discard empty lines, yield only non-empty lines."""
    for line in aFile:
        if len(line) > 0:
            yield line

def splitFields( aFile ):
    """Split a non-empty line into fields."""
    for line in nonEmptyLines(aFile):
        yield line.split()

def dictReader( aFile ):
    """Turn non-empty lines file with header and data into dictionaries.
    Like the ``csv`` module."""
    iter= iter( splitFields( aFile ) )
    heading= iter.next()
    for line in iter:
        yield dict( zip( heading, line ) )

rdr= dictReader( "myFile", "r" )
for d in rdr:
    print d
于 2009-01-30T11:17:36.860 回答