对于变量phoneNumber,它要求完整的电话号码;类似于:029123456。不过,我只需要第一个区号。但是也可以有这样的区号:01845123456,带有不同数量的字符作为区号。我怎样才能把区号存储在变量中呢?你知道吗
下面是full CSV file的一个小示例:
PhoneCode,Area,Example,Latitude,Longitude
113,Leeds,(0113) xxx xxxx,53.801279,-1.548567
114,Sheffield,(0114) xxx xxxx,53.381129,-1.470085
115,Nottingham,(0115) xxx xxxx,52.95477,-1.158086
116,Leicester,(0116) xxx xxxx,52.636878,-1.139759
117,Bristol,(0117) xxx xxxx,51.454513,-2.58791
118,Reading,(0118) xxx xxxx,51.452884,-0.973906
1200,Clitheroe,(01200) xxxxxx,53.871098,-2.393083
1202,Bournemouth,(01202) xxxxxx,50.719164,-1.880769
1204,Bolton,(01204) xxxxxx,53.584441,-2.428619
以下是我目前掌握的代码:
phoneNumber = input("Enter your phone number (UK landline only):")
file = open("phonecodes.csv","r")
#Complete the code here
for line in file:
data = line.split(",")
areaCode = data[0]
if phoneNumber == "0" + areaCode:
print data[1]
file.close()
或者让用户先输入区号,然后分别输入号码,或者让他们先输入区号,然后输入空格,然后输入号码并拆分:
单独:
拆分:
您的数据以逗号分隔,因此csv模块将为您拆分为列。我还使用了原始输入,因为print语句表明您使用的是python2而不是python3。你知道吗
相关问题 更多 >
编程相关推荐