NSNumberFormatter для форматирования американских Номеров телефона

Вот один из способов сделать это. Очень похоже на идею, которую вы описали в своем посте, но это длинный цикл шагов. Возможно, кто-то другой может иметь более короткую реализацию.

import pandas as pd

>>>df
   case RegisterDateTime DischargeDateTime TransferDateTime
0     0      1/1/13 0:12       1/1/13 0:48              NaN
1     1      1/1/13 0:43       1/1/13 2:12              NaN
2     2      1/1/13 0:56       1/1/13 1:22              NaN
3     3      1/1/13 1:04       1/1/13 4:12              NaN
4     4      1/1/13 1:34       1/1/13 4:52              NaN
5     5      1/1/13 2:01               NaN      1/1/13 5:34

# Construct population outflow. This is where you merge Discharges with Transfers
df_out = pd.DataFrame([(j,k) if str(k) != 'nan' else (j,v) for j, k, v in zip(df['case'], df['DischargeDateTime'],df['TransferDateTime'])])
df_out.columns = ['out', 'time']
# You can skip this if your column is already in DateTime
df_out['time'] = pd.to_datetime(df_out['time'])
# Needed for resampling
df_out.set_index('time', inplace=True)
df_out = df_out.resample('H').count().cumsum()
# Needed for merging later
df_out.reset_index(inplace=True)

>>>df_out
                     out
time                    
2013-01-01 00:00:00    1
2013-01-01 01:00:00    2
2013-01-01 02:00:00    3
2013-01-01 03:00:00    3
2013-01-01 04:00:00    5
2013-01-01 05:00:00    6

# Now, repeat for the population inflow
df_in = df.loc[:, ['case', 'RegisterDateTime']]
df_in.columns = ['in', 'time']
df_in['time'] = pd.to_datetime(df_in['time'])
df_in.set_index('time', inplace=True)
df_in = df_in.resample('H').count().cumsum()
df_in.reset_index(inplace=True)

>>>df_in
                     in
time                   
2013-01-01 00:00:00   3
2013-01-01 01:00:00   5
2013-01-01 02:00:00   6


# You can now combine the two
df= pd.merge(df_in, df_out)
df['population'] = df['in'] - df['out']

>>>df
                 time  in  out  population
0 2013-01-01 00:00:00   3    1           2
1 2013-01-01 01:00:00   5    2           3
2 2013-01-01 02:00:00   6    3           3
8
задан samuraisam 20 March 2009 в 05:49
поделиться

3 ответа

I think your issue is that NSNumberFormatter does not support brackets, spaces or dashes. I tried to implement the same method as you and it failed silently and just output unformatted text.

The general problem here is that the iPhone SDK doesn't provide a method to format phone numbers in a locale dependent way.

I have raised bugs with Apple for the following (two of these were duplicates of known issues so I've included Apple's original bug # for those):

#6933244 - Need iPhone SDK interface to format text as locale dependent phone number
#5847381 - UIControl does not support a setFormatter method
#6024985 - Overridden UITextField drawTextInRect method is never called

In an ideal world Apple would provide an NSPhoneNumberFormatter, and you would call setFormatter on your UIControl so it displayed text in a nice pretty way. Unfortunately that doesn't exist on the iPhone.

11
ответ дан 5 December 2019 в 06:54
поделиться

Хорошо номер телефона должен быть 10 символами (11 с продвижением 1), таким образом, необходимо запустить путем изменения этого:

[formatter setPositiveFormat:@"+# (###) ###-####"];

И говоря о продвижении 1, необходимо проверить на это также.

2
ответ дан 5 December 2019 в 06:54
поделиться

. Итак, если вы заинтересованы только в номерах телефонов США, вам нужно будет рассмотреть эти форматы:

+1 (###) ### - ####
1 (###) ### - ####
011 $
### - ####
(###) ### - ####

Мне пришлось сделать что-то подобное, и я поделился результатами, которые я получил здесь: http://the-lost-beauty.blogspot.com/2010/01/LoCale-ssensitive-Phone-Number.html

9
ответ дан 5 December 2019 в 06:54
поделиться