Python编码文本文件,打开它,替换多个部分并输出没有空行作为.csv样式的文本格式

问题描述 投票:0回答:1

我所拥有的是一个文件“test.xls”,它基本上是一个旧的xls(xml格式),在记事本中看起来像这样:

<table cellspacing="1" rules="all" border="1">
    <tr>
        <td>Row A</td><td>Row B</td><td>Row C</td>
    </tr>
    <tr>
        <td>New York</td><td>23</td><td>warm</td>
    </tr>
    <tr>
        <td>San Francisco</td><td>40</td><td>hot</td>
    </tr>
</table>

现在我使用Python将其转换为.text(平面文件),稍后我可以导入到我的MSSQL数据库。

到目前为止我有什么:

import codecs
import os

# read the file with a specific encoding
with codecs.open('test.xls', 'r', encoding = 'ansi') as file_in, codecs.open('test_out.txt', 'w') as file_out:
    lines = file_in.read()
    lines = lines.replace('<tr>', '')

    # save the manipulated data into a new file with new encoding
    file_out.write(lines)

这种方法会产生这样的.text:

Row A;Row B;Row C

New York;23;warm

San Francisco;40;hot

我试图通过多种方法摆脱空行,最后一个是:

for lines in file_in:
        if line != '\n':
            file_out.write(lines)

但文件看起来相同或完全是空的

python text encoding xls
1个回答
0
投票

摆脱空行:

LIST.TXT:

Row A;Row B;Row C

New York;23;warm

San Francisco;40;hot

因此:

logFile = "list.txt"
with open(logFile) as f:
    content = f.readlines()

# to remove empty lines
content = [l.strip() for l in content if l.strip()]
for line in content:
    print(line)

OUTPUT:

Row A;Row B;Row C
New York;23;warm
San Francisco;40;hot

编辑:

也许,从文件中读取然后覆盖它,使用存储结果的列表,稍后可以将其写入文件。

logFile = "list.txt"                # your file name
results = []                        # an empty list to store the lines
with open(logFile) as f:            # open the file
    content = f.readlines()         # read the lines

# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]   # removing the empty lines
for line in content:
    results.append(line)    # appending each line to the list

print(results)              # printing the list


with open(logFile, "w") as f:    # open the file in write mode
    for elem in results:         # for each line stored in the results list
        f.write(str(elem) + '\n')  # write the line to the file
    print("Thank you, your data was overwritten")  # Tadaa-h!
© www.soinside.com 2019 - 2024. All rights reserved.