Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104492
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
| Newsgroups | comp.lang.python |
| Subject | Re: Read and count |
| Date | Thu, 10 Mar 2016 09:23:33 +0000 |
| Lines | 82 |
| Message-ID | <mailman.118.1457601869.15725.python-list@python.org> (permalink) |
| References | <2095750566.7009618.1457559033672.JavaMail.yahoo.ref@mail.yahoo.com> <2095750566.7009618.1457559033672.JavaMail.yahoo@mail.yahoo.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de Wuw6MMdTJdQOYFMuCH6MaQRae6u9XGYGe++0iXeugvyA== |
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.05; 'python)': 0.05; 'builtin': 0.07; 'val': 0.07; 'welcome.': 0.07; 'learner': 0.09; 'observation': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.10; 'output': 0.13; '(moving': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'url:split': 0.16; 'wrote:': 0.16; 'string': 0.17; '2001': 0.18; 'language': 0.19; 'all,': 0.20; "we'd": 0.21; 'latter': 0.22; 'lawrence': 0.22; 'file.': 0.22; 'trying': 0.22; 'seems': 0.23; 'finished': 0.23; 'split': 0.23; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints- To:1': 0.26; 'function': 0.28; 'str': 0.29; "i'm": 0.30; 'print': 0.30; 'comments': 0.30; 'code': 0.30; 'normally': 0.30; "i'd": 0.31; 'another': 0.32; 'language.': 0.32; 'problem': 0.33; 'url:python': 0.33; 'file': 0.34; 'gets': 0.35; 'city.': 0.35; 'easiest': 0.35; "isn't": 0.35; 'needed': 0.36; 'url:org': 0.36; 'possible.': 0.36; 'keyword': 0.36; 'url:library': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'starting': 0.37; 'data': 0.39; 'to:addr:python.org': 0.40; 'mark': 0.40; 'url:3': 0.60; 'your': 0.60; "you'll": 0.61; 'entire': 0.61; 'charset:windows-1252': 0.62; 'leaving': 0.63; 'our': 0.64; 'city': 0.65; 'python-list': 0.66; 'here': 0.66; 'below.': 0.66; '2002': 0.79; "'with'": 0.84; 'pythonistas,': 0.84; 'url:counter': 0.84; 'url:tutorial': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | 80.234.129.0 |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 |
| In-Reply-To | <2095750566.7009618.1457559033672.JavaMail.yahoo@mail.yahoo.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:104492 |
Show key headers only | View raw
Hello and welcome.
Please see my comments below.
On 09/03/2016 21:30, Val Krem via Python-list wrote:
> Hi all,
>
> I am a new learner about python (moving from R to python) and trying read and count the number of observation by year for each city.
>
> The data set look like
> city year x
>
> XC1 2001 10
> XC1 2001 20
> XC1 2002 20
> XC1 2002 10
> XC1 2002 10
>
> Yv2 2001 10
> Yv2 2002 20
> Yv2 2002 20
> Yv2 2002 10
> Yv2 2002 10
>
> out put will be
>
> city
> xc1 2001 2
> xc1 2002 3
> yv1 2001 1
> yv2 2002 3
>
>
> Below is my starting code
> count=0
Seems like you'd want a counter here
https://docs.python.org/3/library/collections.html#collections.Counter.
You'll need to know how to import this so start here
https://docs.python.org/3/tutorial/modules.html
> fo=open("dat", "r+")
We'd normally use the 'with' keyword here so the file automatically gets
closed so:-
with open("dat", "r+") as fo:
> str = fo.read();
'str' isn't a good name as it overrides the builtin function of that
name. This will read the entire file. Easiest to loop as in:-
for line in fo.readlines():
Now you'll need a split call to get at your data
https://docs.python.org/3/library/stdtypes.html#str.split and update
your counter. Once this loop is finished use another loop to produce
your output with print.
> print "Read String is : ", str
The above is for Python 2, it needs parenthesis for Python 3. I'd
recommend starting with the latter if that's possible.
>
> fo.close()
Not needed if you use the 'with' keyword as discussed above.
>
> Many thanks
>
No problem as I'm leaving you to put it all together :)
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Read and count Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-10 09:23 +0000
csiph-web