【问题标题】:How Can I Remove Skipped Lines from Pastebin Output?如何从 Pastebin 输出中删除跳过的行?
【发布时间】:2013-09-24 06:46:33
【问题描述】:

我正在尝试使用 Pastebin 为我托管两个文本文件,以允许我的脚本的任何副本通过 Internet 自行更新。我的代码正在运行,但生成的 .py 文件在每行之间添加了一个空行。这是我的脚本...

import os, inspect, urllib2

runningVersion = "1.00.0v"
versionUrl = "http://pastebin.com/raw.php?i=3JqJtUiX"
codeUrl = "http://pastebin.com/raw.php?i=GWqAQ0Xj"
scriptFilePath = (os.path.abspath(inspect.getfile(inspect.currentframe()))).replace("\\", "/")

def checkUpdate(silent=1):
    # silently attempt to update the script file by default, post messages if silent==0
    # never update if "No_Update.txt" exists in the same folder
    if os.path.exists(os.path.dirname(scriptFilePath)+"/No_Update.txt"):
        return
    try:
        versionData = urllib2.urlopen(versionUrl)
    except urllib2.URLError:
        if silent==0:
            print "Connection failed"
        return
    currentVersion = versionData.read()
    if runningVersion!=currentVersion:
        if silent==0:
            print "There has been an update.\nWould you like to download it?"
        try:
            codeData = urllib2.urlopen(codeUrl)
        except urllib2.URLError:
            if silent==0:
                print "Connection failed"
            return
        currentCode = codeData.read()
        with open(scriptFilePath.replace(".py","_UPDATED.py"), mode="w") as scriptFile:
            scriptFile.write(currentCode)
        if silent==0:
            print "Your program has been updated.\nChanges will take effect after you restart"
    elif silent==0:
        print "Your program is up to date"

checkUpdate()

我剥离了 GUI (wxpython) 并将脚本设置为更新另一个文件而不是实际运行的文件。 “No_Update”位是为了方便工作。

我注意到使用记事本打开生成的文件不会显示跳过的行,使用写字板打开会造成混乱,使用空闲打开会显示跳过的行。基于此,这似乎是一个格式问题,即使“原始”Pastebin 文件似乎没有任何格式。

编辑:我可以删除所有空白行或保持原样没有任何问题,(我已经注意到)但这会大大降低可读性。

【问题讨论】:

    标签: python auto-update pastebin


    【解决方案1】:

    尝试在 open() 中添加二进制限定符:

    with open(scriptFilePath.replace(".py","_UPDATED.py"), mode="wb") as scriptFile:
    

    我注意到你在 pastebin 上的文件是 DOS 格式的,所以里面有\r\n。当您调用scriptFile.write() 时,它会将\r\n 转换为\r\r\n,这非常令人困惑。

    open() 中指定"b" 将导致脚本文件跳过该转换并将文件写入为DOS 格式。

    或者,您可以确保 pastebin 文件中只有 \n,并在脚本中使用 mode="w"

    【讨论】:

    • 谢谢,这完全缓解了问题。您是如何确定 pastebin 文件的格式的?
    • 使用我的浏览器,我将文件下载到我的 Linux PC。在 shell 中,我使用 filevim 来确定文件类型(我不记得是哪一个)。
    猜你喜欢
    • 2020-06-04
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2013-06-09
    相关资源
    最近更新 更多