Python隐式将四位字符串转换为整数

0 投票
3 回答
522 浏览
提问于 2025-04-17 10:22

我现在正在写一个脚本,从两个来源提取数据,其中一个来源是挪威的邮政编码。挪威的邮政编码由四个数字组成,有些编码是以零开头的。

以下是代码:

#This section loads data on Norwegian post codes and places into a dictionary where postcode is key
f = open("postoversikt.txt", "r");
f1 = open("PCODES_USER_TRIM.txt","r") #load the file with all the users. 
fo = open("pcodes_out","w")
place = {}
times = {}
for line in f:
    words = line.rsplit("\t");
    place[str(words[0])] = words[1]; #Reverse these to change the key and value - Default key: postcode value: place

number = 0;
number_unique = 0;
number_alike = 0;

for line in f1:
    number = number + 1;
    words1 = line.rsplit(";");
    if not words1[1] in times:
        number_unique = number_unique + 1;
        times[words1[1]] = 1;
    else: 
        number_alike = number_alike + 1;
        times[words1[1]] = times[words1[1]] + 1;

for key, value in times.items():
     print key+";"+value+";"+words[key];
     fo.write(key+";"+value+";"+words[key]+"\n");


print "Totalt antall objekter behandlet er: "+ str(number);
print "Hvorav antall unike var: "+ str(number_unique);
print "Antall like nummer ble funnet: " + str(number_alike);

来自 PCODES_USER_TRIM 的一些行:

75621;4517;45 - 65
35214;7650;25 - 45
55624;9015;25 - 45
09523;5306;45 - 65
09051;2742;25 - 45
88941;1661;18 - 25

来自 postoversikt.txt 的一些行:

0001    OSLO    0301    OSLO    P
0010    OSLO    0301    OSLO    B
0015    OSLO    0301    OSLO    K
0016    OSLO    0301    OSLO    K
0017    OSLO    0301    OSLO    K
0018    OSLO    0301    OSLO    G
0021    OSLO    0301    OSLO    K
0022    OSLO    0301    OSLO    K

我遇到的一个问题是,以零开头的邮政编码在处理时会丢失这个零。我猜这是因为它被内部转换成了整数(我刚开始学习 Python,所以如果我的问题有点基础,请多多包涵)。我希望这些编码能保持标准格式,即四个数字 xxxx。我的第二个问题,可能是由第一个问题引起的,就是我想在最终的输出中添加邮政编码的名称。但这不行,因为我无法使用键来引用对应的名称。

我以前是通过使用 str() 方法将要打印的对象转换为字符串,但在当前版本中我没有这样做,因为我想从根本上解决这个问题。

有没有人能帮我解决这个小问题?我该如何使用 rsplit 将字符串放入字典中,而不将其转换为整数呢?

3 个回答

0

Python会把四位数的数字(比如0004)变成4,这其实没什么大问题,只要你计算的结果是对的就行。

你需要做的就是把输出格式化成你想要的样子。例如:

i=4
print "%4d" % i

这样会得到结果:0004

i=1254
print "%04d" % i

这样会得到结果:1254

关于Python中字符串格式化的更多细节,可以查看这里: http://docs.python.org/release/2.4.4/lib/typesseq-strings.html

4

如果你想把一个整数格式化成至少4位数的样子(前面用零填充),你可以这样做:

integer = 5
s = "%04d" % integer
2

Python是一种“强类型”的语言,也就是说它不会自动把不同类型的数据转换成其他类型:

>>> d = {'01234':'value'}
>>> print d.items()
[('01234', 'value')]

我在你的代码里没有看到任何将数据转换成int的地方,但我敢肯定这不是你实际使用的代码,因为它至少有一个语法错误:

 fo.write("key+";"+value+";"+words[key]\n")

请把你实际使用的代码粘贴过来。

另外,给我们提供几行输入文档和它们的格式,这样我们就不用猜了。

编辑:

这段代码会实现你想要的功能。再说一次,没有迹象表明前导零会丢失...

places = {}
for line in f:
    post, place, _rest = line.split('\t',2)
    places[post] = place
f.close()

times = {}
for line in f1:
    _id, post, _rest = line.split(';',2)
    times[post] = times.get(post, 0) + 1
f1.close()

for k,v in times.iteritems():
    fo.write("%s;%s;%s\n" % (k,v,places[k]))
fo.close()

number = sum(times.itervalues())
number_unique = len(times)
number_alike = number - number_unique

print number, number_unique, number_alike

撰写回答