Python办公,如何遍历“数据库导出的表格”的所有单元格,清除单元格内容的前后空字符串?

Python026

Python办公,如何遍历“数据库导出的表格”的所有单元格,清除单元格内容的前后空字符串?,第1张

由于没有看到导出的表格样本,直接说吧:

遍历建议直接用pandas的itertuples(),去除前面的空白字符串用lstrip()就行

df = pd.read_excel("test.xlsx")

for row in df.itertuples():

row.行名称=row.行名称.lstrip()

以上应该就可以了,注意缩进。

按 指定行数 分页读取好处理:

def iterpage(istream, pagesize):

    buffer = []

    for data in istream:

        buffer.append(data)

        if len(buffer)>=pagesize:

            yield buffer

            buffer = []

    if buffer:

        yield buffer

with open("source.txt", 'rt') as handle:

    for page in iterpage(handle, 1000):

        print page  # or your business logical

        print "-"*32  # page break

删除文本文件的前N行:

def removehead(filename, headlines):

    buffer = []

    with open(filename, 'rt') as handle:

        for i, ln in enumerate(handle):

            if ln < headlines:

                continue

            buffer.append(ln)

    

    with open(filename, 'wt') as handle:

        handle.writelines(buffer)

或者:

def getandremovehead(filename, headlines):

    with open(filename, 'rt') as handle:

        buffer = handle.readlines()

    with open(filename, 'wt') as handle:

        handle.writelines(buffer[headlines:])

    return buffer[:headlines]

但遇到大文本文件时,删除其中N行不是很理想的业务方案

遍历数据结构的时候,是不可以修改原数据结构的。不然就会抛出错误。

我常用的解决办法是做一份拷贝,遍历这个拷贝。(如果数据不是很大的话)

比如,这个代码

C#代码

1.<SPAN style="FONT-SIZE: x-small">IDictionary<int, string>ht = new Dictionary<int, string>()

2.ht.Add(1, "one")

3.ht.Add(2, "two")

4.

5.// Print "one,two"

6.Console.WriteLine(String.Join(",", ht.Values.Select(i =>i.ToString()).ToArray()))

7.

8.foreach (int key in new List<int>(ht.Keys)) {

9.if (key == 1) ht.Remove(key)

10.}

11.

12.// Print "two"

13.Console.WriteLine(String.Join(",", ht.Values.Select(i =>i.ToString()).ToArray()))</SPAN>

IDictionary<int, string>ht = new Dictionary<int, string>()

ht.Add(1, "one")

ht.Add(2, "two")

// Print "one,two"

Console.WriteLine(String.Join(",", ht.Values.Select(i =>i.ToString()).ToArray()))

foreach (int key in new List<int>(ht.Keys)) {

if (key == 1) ht.Remove(key)

}

// Print "two"

Console.WriteLine(String.Join(",", ht.Values.Select(i =>i.ToString()).ToArray()))我在遍历的时候,做了一份拷贝。代码是 new List<int>(ht.Keys),用到了 List 的构造拷贝函数,