Hi
A short newbie question. I would like to extract some values from a
given text file directly into python variables. Can this be done simply
by either standard library or other libraries? Some pointers where to
get started would be much appreciated.
An example text file:
---
Some text t
list.txt is a file that contains the following lines:
Apples 34
Bananas 10
Oranges 56
>>> file = open("list.txt","r")
>>> mystring = file.read()
>>> mystring
'Apples 34 \nBananas 10\nOranges 56 '
>>> mylist = mystring.split('\n')
>>> mylist
['Apples 34 ', 'Bananas 10', 'Oranges 56 ']
>>> mydict =
First try, probably there are better ways to do it, and it's far from
resilient, it breaks in lot of different ways (example: more than one
number in one line, number with text on both sides of the line, etc.)
I have divided the data munging in many lines so I can see what's
happening, and you can
P.S.
>>> file.close()
MTD wrote:
> list.txt is a file that contains the following lines:
> Apples 34
> Bananas 10
> Oranges 56
>
> >>> file = open("list.txt","r")
> >>> mystring = file.read()
> >>> mystring
> 'Apples 34 \nBananas 10\nOranges 56 '
> >>> mylist = mystring.split('\n')
> >>> mylist
>
> What I first though was if there was possible to make a filter such as:
>
> Apples (apples)
> (ducks) Ducks
> (butter) g butter
Try something like:
import re
text = """> Some text that can span some lines.
Apples 34
56 Ducks
Some more text.
"""
filters = {"apples": re.compile(
"Preben Randhol" <[EMAIL PROTECTED]> wrote in message
news:[EMAIL PROTECTED]
> What I first though was if there was possible to make a filter such as:
>
> Apples (apples)
> (ducks) Ducks
> (butter) g butter
>
> The data can be put in a hash table.
>
> Or maybe there are better ways? I general
Thus spoke Preben Randhol (on 2006-06-16 10:36):
> A short newbie question. I would like to extract some values from a
> given text file directly into python variables. Can this be done simply
> by either standard library or other libraries? Some pointers where to
> get started would be much appre
Thus spoke Mirco Wahab (on 2006-06-16 21:21):
> I used your example just to try that in python
> (i have to improve my python skills), but waved
> the white flag after realizing that there's no
> easy string/var-into-string interpolation.
I did another try on it, using all my Python
resources ava
On Sat, 17 Jun 2006 14:20:44 +0200
Mirco Wahab <[EMAIL PROTECTED]> wrote:
> Thus spoke Mirco Wahab (on 2006-06-16 21:21):
>
> > I used your example just to try that in python
> > (i have to improve my python skills), but waved
> > the white flag after realizing that there's no
> > easy string/var
Thus spoke Preben Randhol (on 2006-06-17 23:25):
> The code is a very good starting point for me! I already
> managed to change it and I see I need to make it a bit more robust.
I think, the only thing you have to look at - is
the congruence of the regex-based filter rule and the text.
suppose
Thus spoke Dennis Lee Bieber (on 2006-06-18 06:29):
> On Sun, 18 Jun 2006 03:12:23 +0200, Mirco Wahab
> <[EMAIL PROTECTED]> declaimed the following in comp.lang.python:
>> - you have to explicitly instantiate a dictionary value
>> (with 0) if/before you want in-place add to it (why is that?)
> U
On Sun, 18 Jun 2006 10:54:01 +0200
Mirco Wahab <[EMAIL PROTECTED]> wrote:
>
> For the other issue I stumbled upon:
>
> - no DWIM-ism (do what I mean) on 'value' addition
>
> a = '1'
> a += '1.'
> print a
>
> will print
> 11.
>
> and not 2., as in 'dynamically ty
Thus spoke Preben Randhol (on 2006-06-18 13:34):
> On Sun, 18 Jun 2006 10:54:01 +0200
> Mirco Wahab <[EMAIL PROTECTED]> wrote:
>> - no DWIM-ism (do what I mean) on 'value' addition
>
> But you don't add two values. you add two strings. If you
> want numbers you must convert the strings.
Why? At
In <[EMAIL PROTECTED]>, Mirco Wahab wrote:
> You see the picture? Pythons designer made the
> same mistake as the Java/Javascript designer -
> they use the _same_ operator (+) for number _addition_
> and string _concatenation_, which is, imho, cumbersome.
And ``+`` means also list/tuple concatena
Thus spoke Marc 'BlackJack' Rintsch (on 2006-06-18 18:54):
> In <[EMAIL PROTECTED]>, Mirco Wahab wrote:
>> they use the _same_ operator (+) for number _addition_
>> and string _concatenation_, which is, imho, cumbersome.
>
> And ``+`` means also list/tuple concatenation and really anything for us
On Sun, 18 Jun 2006 17:46:43 +0200
Mirco Wahab <[EMAIL PROTECTED]> wrote:
> Thus spoke Preben Randhol (on 2006-06-18 13:34):
> > On Sun, 18 Jun 2006 10:54:01 +0200
> > Mirco Wahab <[EMAIL PROTECTED]> wrote:
> >> - no DWIM-ism (do what I mean) on 'value' addition
> >
> > But you don't add two valu
Thus spoke Dennis Lee Bieber (on 2006-06-18 22:37):
> The only cure for that is complete and painful bone marrow
> transplant As a start, after six months of no PERL go back and try
> reading some of your code.
Uhhh, this is like giving the
mounted knight a longbow and
push him onto the ba
17 matches
Mail list logo