split function splitting on spaces when data contains spaces also

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

split function splitting on spaces when data contains spaces also

fugee ohu
I'm trying to split on spaces but one of the data columns has spaces in it and no quotes around it It's sms exported from my phone  I wanna save them to a table Each line looks like the line below The first is the phone number then the date and time, then the body of the sms message and then a single digit 1 or 2 to indicate if the message was sent or received Looks like between time and message body there's a tab delimeter but the other delimeters are single spaces but then the message body contains single spaces and so does the date and time column How can I split this? 

+2124914028 06/09/2017 12:21:56 Are we meeting up for breakfast today? 1

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f201139b-f56b-4909-af53-5144e79dbef3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: split function splitting on spaces when data contains spaces also

Colin Law
Can't you just use regular expressions to split it up?  Like this for example
https://www.oreilly.com/library/view/scala-cookbook/9781449340292/ch01s09.html

Colin
On Sun, 4 Nov 2018 at 21:18, fugee ohu <[hidden email]> wrote:

>
> I'm trying to split on spaces but one of the data columns has spaces in it and no quotes around it It's sms exported from my phone  I wanna save them to a table Each line looks like the line below The first is the phone number then the date and time, then the body of the sms message and then a single digit 1 or 2 to indicate if the message was sent or received Looks like between time and message body there's a tab delimeter but the other delimeters are single spaces but then the message body contains single spaces and so does the date and time column How can I split this?
>
> +2124914028 06/09/2017 12:21:56 Are we meeting up for breakfast today? 1
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f201139b-f56b-4909-af53-5144e79dbef3%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLu%3D5A7xOmSrHS0QuH-Sy6tcfi3kfO%3D2pQ%3DABLTqC81y8A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: split function splitting on spaces when data contains spaces also

botp
In reply to this post by fugee ohu
On Mon, Nov 5, 2018 at 5:18 AM fugee ohu <[hidden email]> wrote:
I'm trying to split on spaces but one of the data columns has spaces in it and no quotes around it It's sms exported from my phone  I wanna save them to a table Each line looks like the line below The first is the phone number then the date and time, then the body of the sms message and then a single digit 1 or 2 to indicate if the message was sent or received Looks like between time and message body there's a tab delimeter but the other delimeters are single spaces but then the message body contains single spaces and so does the date and time column How can I split this? 

+2124914028 06/09/2017 12:21:56 Are we meeting up for breakfast today? 1

just split it by parts (simpler for me).

eg,
> s="+2124914028 06/09/2017 12:21:56 Are we meeting up for breakfast today? 1"
=> "+2124914028 06/09/2017 12:21:56 Are we meeting up for breakfast today? 1"
> phone,date,time,remaining=s.split(/\s/,4)
=> ["+2124914028", "06/09/2017", "12:21:56", "Are we meeting up for breakfast today? 1"]

the remaining part is similar.
best regards
--botp

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAAwHHQhDkcPQxaqDS4RKDSMqbFEgW%3DW4SZ427bm14xo5%2BXRPkA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: split function splitting on spaces when data contains spaces also

fugee ohu


On Monday, November 5, 2018 at 7:08:27 AM UTC-5, bot Peña wrote:
On Mon, Nov 5, 2018 at 5:18 AM fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="OVqT82MQBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
I'm trying to split on spaces but one of the data columns has spaces in it and no quotes around it It's sms exported from my phone  I wanna save them to a table Each line looks like the line below The first is the phone number then the date and time, then the body of the sms message and then a single digit 1 or 2 to indicate if the message was sent or received Looks like between time and message body there's a tab delimeter but the other delimeters are single spaces but then the message body contains single spaces and so does the date and time column How can I split this? 

+2124914028 06/09/2017 12:21:56 Are we meeting up for breakfast today? 1

just split it by parts (simpler for me).

eg,
> s="+2124914028 06/09/2017 12:21:56 Are we meeting up for breakfast today? 1"
=> "+2124914028 06/09/2017 12:21:56 Are we meeting up for breakfast today? 1"
> phone,date,time,remaining=s.split(/\s/,4)
=> ["+2124914028", "06/09/2017", "12:21:56", "Are we meeting up for breakfast today? 1"]

the remaining part is similar.
best regards
--botp


Thanks

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/484bec01-c123-4ab8-855f-f0106923d98b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: split function splitting on spaces when data contains spaces also

fugee ohu
In reply to this post by Colin Law


On Sunday, November 4, 2018 at 4:52:30 PM UTC-5, Colin Law wrote:
Can't you just use regular expressions to split it up?  Like this for example
<a href="https://www.oreilly.com/library/view/scala-cookbook/9781449340292/ch01s09.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.oreilly.com%2Flibrary%2Fview%2Fscala-cookbook%2F9781449340292%2Fch01s09.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGlsqepTEwCefVqDCuFZ9Jn0tb0Aw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.oreilly.com%2Flibrary%2Fview%2Fscala-cookbook%2F9781449340292%2Fch01s09.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGlsqepTEwCefVqDCuFZ9Jn0tb0Aw&#39;;return true;">https://www.oreilly.com/library/view/scala-cookbook/9781449340292/ch01s09.html

Colin
On Sun, 4 Nov 2018 at 21:18, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="Btu5k67hAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:

>
> I'm trying to split on spaces but one of the data columns has spaces in it and no quotes around it It's sms exported from my phone  I wanna save them to a table Each line looks like the line below The first is the phone number then the date and time, then the body of the sms message and then a single digit 1 or 2 to indicate if the message was sent or received Looks like between time and message body there's a tab delimeter but the other delimeters are single spaces but then the message body contains single spaces and so does the date and time column How can I split this?
>
> +2124914028 06/09/2017 12:21:56 Are we meeting up for breakfast today? 1
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Btu5k67hAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="Btu5k67hAwAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/f201139b-f56b-4909-af53-5144e79dbef3%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/f201139b-f56b-4909-af53-5144e79dbef3%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/f201139b-f56b-4909-af53-5144e79dbef3%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/f201139b-f56b-4909-af53-5144e79dbef3%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.

The file was all one line with the data delimited by tabs so i wrote a counter routine to treat every multiple of 5 as 1 Worked but now I have a lot of phone numbers formatted differently +19173320500 (808) 741-1112 7184414411 see what I mean How can I uniform all these phone numbers so they'll list ordered by phone num

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b609fa47-8895-4681-8829-6edf7098ffdb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: split function splitting on spaces when data contains spaces also

Walter Lee Davis

> On Nov 5, 2018, at 9:36 AM, fugee ohu <[hidden email]> wrote:
>
>
>
> On Sunday, November 4, 2018 at 4:52:30 PM UTC-5, Colin Law wrote:
> Can't you just use regular expressions to split it up?  Like this for example
> https://www.oreilly.com/library/view/scala-cookbook/9781449340292/ch01s09.html 
>
> Colin
> On Sun, 4 Nov 2018 at 21:18, fugee ohu <[hidden email]> wrote:
> >
> > I'm trying to split on spaces but one of the data columns has spaces in it and no quotes around it It's sms exported from my phone  I wanna save them to a table Each line looks like the line below The first is the phone number then the date and time, then the body of the sms message and then a single digit 1 or 2 to indicate if the message was sent or received Looks like between time and message body there's a tab delimeter but the other delimeters are single spaces but then the message body contains single spaces and so does the date and time column How can I split this?
> >
> > +2124914028 06/09/2017 12:21:56 Are we meeting up for breakfast today? 1
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> > To post to this group, send email to [hidden email].
> > To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/f201139b-f56b-4909-af53-5144e79dbef3%40googlegroups.com.
> > For more options, visit https://groups.google.com/d/optout.
>
> The file was all one line with the data delimited by tabs so i wrote a counter routine to treat every multiple of 5 as 1 Worked but now I have a lot of phone numbers formatted differently +19173320500 (808) 741-1112 7184414411 see what I mean How can I uniform all these phone numbers so they'll list ordered by phone num

Are the numbers all in the US, or all in another country format? There's an ActiveSupport helper in Rails that can format phone numbers, but it requires that you pass it the country as a parameter (or it defaults to US, I think).

https://api.rubyonrails.org/classes/ActiveSupport/NumberHelper.html#method-i-number_to_phone

Walter

>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
> To post to this group, send email to [hidden email].
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b609fa47-8895-4681-8829-6edf7098ffdb%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6462A392-0336-4898-8DE2-77BD971B005F%40wdstudio.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: split function splitting on spaces when data contains spaces also

fugee ohu


On Tuesday, November 6, 2018 at 9:06:30 AM UTC-5, Walter Lee Davis wrote:

> On Nov 5, 2018, at 9:36 AM, fugee ohu <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="IyTgq2llBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">fuge...@...> wrote:
>
>
>
> On Sunday, November 4, 2018 at 4:52:30 PM UTC-5, Colin Law wrote:
> Can't you just use regular expressions to split it up?  Like this for example
> <a href="https://www.oreilly.com/library/view/scala-cookbook/9781449340292/ch01s09.html" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.oreilly.com%2Flibrary%2Fview%2Fscala-cookbook%2F9781449340292%2Fch01s09.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGlsqepTEwCefVqDCuFZ9Jn0tb0Aw&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fwww.oreilly.com%2Flibrary%2Fview%2Fscala-cookbook%2F9781449340292%2Fch01s09.html\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGlsqepTEwCefVqDCuFZ9Jn0tb0Aw&#39;;return true;">https://www.oreilly.com/library/view/scala-cookbook/9781449340292/ch01s09.html
>
> Colin
> On Sun, 4 Nov 2018 at 21:18, fugee ohu <[hidden email]> wrote:
> >
> > I'm trying to split on spaces but one of the data columns has spaces in it and no quotes around it It's sms exported from my phone  I wanna save them to a table Each line looks like the line below The first is the phone number then the date and time, then the body of the sms message and then a single digit 1 or 2 to indicate if the message was sent or received Looks like between time and message body there's a tab delimeter but the other delimeters are single spaces but then the message body contains single spaces and so does the date and time column How can I split this?
> >
> > +2124914028 06/09/2017 12:21:56 Are we meeting up for breakfast today? 1
> >
> > --
> > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> > To post to this group, send email to [hidden email].
> > To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/f201139b-f56b-4909-af53-5144e79dbef3%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/f201139b-f56b-4909-af53-5144e79dbef3%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/f201139b-f56b-4909-af53-5144e79dbef3%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/f201139b-f56b-4909-af53-5144e79dbef3%40googlegroups.com.
> > For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.
>
> The file was all one line with the data delimited by tabs so i wrote a counter routine to treat every multiple of 5 as 1 Worked but now I have a lot of phone numbers formatted differently +19173320500 (808) 741-1112 7184414411 see what I mean How can I uniform all these phone numbers so they'll list ordered by phone num

Are the numbers all in the US, or all in another country format? There's an ActiveSupport helper in Rails that can format phone numbers, but it requires that you pass it the country as a parameter (or it defaults to US, I think).

<a href="https://api.rubyonrails.org/classes/ActiveSupport/NumberHelper.html#method-i-number_to_phone" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fapi.rubyonrails.org%2Fclasses%2FActiveSupport%2FNumberHelper.html%23method-i-number_to_phone\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFs7uz_KEWdtD-Wj65XobJpwWxV6g&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fapi.rubyonrails.org%2Fclasses%2FActiveSupport%2FNumberHelper.html%23method-i-number_to_phone\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFs7uz_KEWdtD-Wj65XobJpwWxV6g&#39;;return true;">https://api.rubyonrails.org/classes/ActiveSupport/NumberHelper.html#method-i-number_to_phone

Walter

>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="IyTgq2llBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="IyTgq2llBAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">rubyonra...@googlegroups.com.
> To view this discussion on the web visit <a href="https://groups.google.com/d/msgid/rubyonrails-talk/b609fa47-8895-4681-8829-6edf7098ffdb%40googlegroups.com" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/b609fa47-8895-4681-8829-6edf7098ffdb%40googlegroups.com&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/msgid/rubyonrails-talk/b609fa47-8895-4681-8829-6edf7098ffdb%40googlegroups.com&#39;;return true;">https://groups.google.com/d/msgid/rubyonrails-talk/b609fa47-8895-4681-8829-6edf7098ffdb%40googlegroups.com.
> For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.


Thanks  I used string.gsub(/\D/, ''/) to strip out the non numerics and then string.gsub(/^1/''/) to strip off the leading 1

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/407d71ce-35cc-4d40-b42c-53f033255664%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.