第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python腳本在Windows中輸出字典數(shù)據(jù),但在Linux中不輸出

Python腳本在Windows中輸出字典數(shù)據(jù),但在Linux中不輸出

FFIVE 2021-04-02 13:10:17
我正在編寫腳本來檢查兩個文件之間的ID重疊,在Windows中,它能夠從{ID:filepath}的字典中輸出ID列表的文件路徑。但是,在我的Linux服務(wù)器中,沒有輸出。CELs=[]CELpaths = {}f=open(sys.argv[1], 'r')data = f.read()lines = data.split('\n')[1:-1]for line in lines:    tabs = line.split('\\')    CELs.append(tabs[-1])    CELpaths[(tabs[-1])]=lineyyid = []f2=open(sys.argv[2], 'r')data2=f2.read()lines2=data2.split('\n')for x in lines2:    yyid.append(x)for c in yyid:    if c in CELpaths:        print (CELpaths[c])問題肯定在“ yyid中的c:”段中,在Linux服務(wù)器上的Python無法執(zhí)行“ if C in CELs:”行。我的Linux運(yùn)行的是Python 2.7,而我的Windows運(yùn)行的是Python3。這僅僅是版本問題嗎?有沒有辦法修復(fù)語法以允許在Linux上輸出?
查看完整描述

1 回答

?
慕絲7291255

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個贊

使用正確的方法逐行讀取文件(這通常是遍歷文件對象),使用line.strip()(不帶參數(shù))刪除換行符(無論是什么),并記住這一點(diǎn),您可能會遇到較少的問題。 Python“ \”是一個轉(zhuǎn)義字符,因此“ \”實(shí)際上是“ \”(如果要兩個反斜杠,請使用原始字符串ie r"\\")。


另外,Python不會保證打開的文件將被關(guān)閉,因此您必須注意這一點(diǎn)。


未經(jīng)測試(當(dāng)然,因?yàn)槟鷽]有發(fā)布源數(shù)據(jù)),但這主要是腳本的pythonic等效項(xiàng):


def main(p1, p2):

    CELs=[]

    CELpaths = {}


    with open(p1) as f:

        # skip first line

        f.next() 

        for line in f:

            # remove trailing whitespaces (including the newline character) 

            line = line.rstrip() 

            # I assume the `data[1:-1] was to skip an empty last line

            if not line:

                continue

            # If you want a single backward slash, 

            # this would be better expressed as `r'\'`

            # If you want two, you should use `r'\\'.

            # AND if it's supposed to be a platform-dependant

            # filesystem path separator, you should be using

            # `os.path.sep` and/or `os.path` functions.  

            tabs = line.split('\\')

            CELs.append(tabs[-1])

            CELpaths[(tabs[-1])] = line


    with open(p2) as f:

        # no need to store the whole stuff in memory

        for line in f:

            line = line.strip()

            if line in CELpaths:

                print (CELpaths[line])


if __name__ == "__main__":

    import sys

    p1, p2 = sys.argv[1], sys.argv[2]

    main(p1, p2)

我當(dāng)然不能保證它會解決您的問題,因?yàn)槟鷽]有提供MCVE ...


查看完整回答
反對 回復(fù) 2021-04-16
  • 1 回答
  • 0 關(guān)注
  • 329 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號