小编use*_*193的帖子

Bash脚本将Windows路径转换为linux路径

我是shell脚本的新手,并尝试完成以下操作,将Windows路径转换为linux路径并导航到该位置:

输入:cdwin "J:\abc\def" 动作:cd /usr/abc/def/

所以,我正在改变以下内容:

"J:" -> "/usr"
Run Code Online (Sandbox Code Playgroud)

和

"\" -> "/"
Run Code Online (Sandbox Code Playgroud)

这是我的尝试,但它不起作用.如果我回应它,​​它只返回一个空白:

function cdwin(){
    line="/usrfem/Projects$1/" | sed 's/\\/\//g' | sed 's/J://'
    cd $line
}
Run Code Online (Sandbox Code Playgroud)

linux windows bash sed path

5
推荐指数
1
解决办法
1万
查看次数

Python 3嵌套尝试词典中键的异常

有没有一种很好的方法来处理不存在的字典键?

我有一个基于字典的数据库,其中的键看起来像这样

ID
   ver
      sub_ver
           A
           B
           C
Run Code Online (Sandbox Code Playgroud)

我想比较每个ID/ver/sub_ver的A,B,C键的值,以确保:

 (A==B and C==None) or (A==C and B==None) or (A==B==C)
Run Code Online (Sandbox Code Playgroud)

然而,并非所有"ID"都具有A和B以及C键/值

我不是很好的代码:

**loops outside of this for ID/ver/sub_ver** 
try:
    A = data_structure[ID][ver][sub_ver]['A']
    B = data_structure[ID][ver][sub_ver]['B']
    C = data_structure[ID][ver][sub_ver]['C']
except KeyError:
    try:
        A = data_structure[ID][ver][sub_ver]['A']
        B = data_structure[ID][ver][sub_ver]['B']
        C = None

    except KeyError: 
        try:
            A = data_structure[ID][ver][sub_ver]['A']
            B = None
            C = data_structure[ID][ver][sub_ver]['C']                    
Run Code Online (Sandbox Code Playgroud)

接下来我检查所有值是否匹配

我使用set()以防万一A/B/C列表不合规

if not any((set(A)==set(B) and C==None, \
            set(A)==set(C) and B==None, \
            set(A)==set(B)==set(C))):
    set_of_problems.append([ID, ver, sub_ver, [A, B, C])
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来执行嵌套try …

python dictionary key try-catch

0
推荐指数
1
解决办法
332
查看次数

标签 统计

bash ×1

dictionary ×1

key ×1

linux ×1

path ×1

python ×1

sed ×1

try-catch ×1

windows ×1