繁体   English   中英

如何从Pastebin输出中删除跳过的行?

[英]How Can I Remove Skipped Lines from Pastebin Output?

我正在尝试使用Pastebin为我托管两个文本文件,以允许脚本的任何副本通过互联网进行更新。 我的代码正常工作,但是生成的.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文件似乎没有任何格式,这似乎也是一个格式问题。

编辑:我可以只剥去所有空白行或将其保留为没有任何问题,(我注意到),但这会大大降低可读性。

尝试在您的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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM