以下代码可以实现我想要的功能,但是还有其他更多的python风格吗?
具有以下格式的文件:
...
x_mapUser Number := 0
...
OPEN cur;
LOOP FETCH cur INTO l_rec;
EXIT WHEN cur%notfound;
UPDATE table_U SET USER_ID = V1(c_obj_id) WHERE CURRENT OF cur;
IF x_mapUser > mapUser-1 AND c_obj_id < count_id Then
x_mapUser := 0;
c_obj_id := c_obj_id +1;
End if;
x_mapUser := x_mapUser +1;
END LOOP;
CLOSE cur;
和代码:
key1:value1,key2:value2,...
key21:value21,key22:value22,...
.
EOF
谢谢
答案 0 :(得分:4)
一种更快,更Python化的方法是使用csv
模块(默认情况下用逗号分隔),然后将项目拆分成双重展平的生成器理解,并馈送到dict
中,该表达式接受具有2个元素的元组:>
import csv
with open("test.csv",newline="") as f: # replace ,newline="" by ,"rb" in python 2
cr = csv.reader(f)
d = dict(x.split(":") for row in cr for x in row)
print(d)
结果:
{'key1': 'value1', 'key22': 'value22', 'key21': 'value21', 'key2': 'value2'}
非csv版本:
import csv
with open("test.csv") as f:
d = dict(x.split(":") for line in f for x in line.split(","))
答案 1 :(得分:1)
使用varchar
:
list.txt:
split()
因此:
key1:value1,key2:value2,key3:value3
key21:value21,key22:value22
输出:
with open("list.txt") as fileObj:
content = fileObj.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
for line in content:
for elem in line.split(","):
print({elem .split(":")[0] : elem.split(":")[1]})
OR
如果要将它们存储在字典中:
{'key1': 'value1'}
{'key2': 'value2'}
{'key3': 'value3'}
{'key21': 'value21'}
{'key22': 'value22'}