小编Sli*_*fez的帖子

删除文件python的空格

我有一个包含多行的文件,其中一些有空格.

x=20
y=3
z = 1.5
v = 0.1
Run Code Online (Sandbox Code Playgroud)

我想删除这些空格并将每一行放入字典中,其中'='符号前面的元素是键,而'='符号后面的元素将是它的值.

但是,我的代码不起作用,至少是"删除空格"部分.这是代码:

def copyFile(filename):
    """
    function's contract
    """
    with open(filename, 'r') as inFile:
        for line in inFile:
            cleanedLine = line.strip()
            if cleanedLine:
                firstPart, secondPart = line.split('=')  
                dic[firstPart] = float(secondPart)
        inFile.close()
    return dic
Run Code Online (Sandbox Code Playgroud)

清空空格后,我的文件应该是这样的

x=20
y=3
z=1.5
v=0.1
Run Code Online (Sandbox Code Playgroud)

但是没有用.我究竟做错了什么?

python spaces file

3
推荐指数
1
解决办法
74
查看次数

标签 统计

file ×1

python ×1

spaces ×1