[Tutor] Converting a txt file to dictionary
nzbz xx
nzbzxx at gmail.com
Sat Aug 29 02:30:47 EDT 2020
I'm trying to create a def function code to validate user input. I would
want the user to only input either 1 or 2. Any other inputs would reprompt
the user again. This is what i have so far:
def data_validation(User_Decision):
try:
if User_Decision != 1 or User_Decision != 2:
print("Please enter option number: 1 or 2.")
except ValueError:
print("Input is invalid. Please try again.")
User_Decision = int(input(">"))
while data_validation(User_Decision):
User_Decision = input(">")
On Thu, Aug 27, 2020 at 1:30 AM Peter Otten <__peter__ at web.de> wrote:
> nzbz xx wrote:
>
> Interesting name ;)
>
> > I have a plantrecords.txt file with the following format:
> >
> > Plantname
> > date & time of record
> > description
> > description
> > description
> > (blank line)
> > Plantname
> > date & time of record
> > description
> > description
> > description
> > (blank line)
> > .
> > .
> > .
> >
> >
> > How do I convert this text file into a dictionary such that it gives me
> > dict = {'plantname': [datetimerecord, description, description,
> > description], 'plantname': [datetime.....]} so on and so forth
>
>
> import itertools
> import pprint
>
> filename = "plants.txt"
> plants = {}
> with open(filename) as instream:
> for key, group in itertools.groupby(map(str.strip, instream),
> key=bool):
> if key:
> name, *rest = group
> plants[name] = rest
>
> pprint.pprint(plants)
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list