Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
namedtuples and text files
#1
Is it possible to get the values out of a namedtuple that has been stored as a text file?

I am able to get the values out when the namedtuple is first made but, after storing it that approach fails.

Thanks for any help

some.txt file
HighScores(name='aname', score=850)
HighScores(name='anothername', score=230)
HighScores(name='yetanother', score=50)
This bit works when creating the file but, not after being stored in a text file
HighScores = namedtuple("HighScores", "name score")
name = HighScores(name, score)
print(getattr(name, "name"))
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#2
For me it is unclear what do you want to accomplish or what the exact problem is.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
I'm storing the values in a text file keeping the namedtuple format for later sorting. The text file will be changing at various intervals.
I'm trying to pull the values out of each line of the files for display. Name and score. I was wondering if can get those values or if I need to take another approach at storing the data?

Thanks
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
I don't have context but it seems that rows of your sample some.txt file doesn't have any benefit being stored as namedtuple.

You can save data to file just as two comma separated values per row and read back into whatever datastructure (including namedtuple).

If there are more than one instance of namedtuple you should consider keeping them in some datastructure (list), otherwise access to them will be complicated.

This will enable you do:

>>> HighScores = namedtuple("HighScores", "name score")
>>> lst = [HighScores(name, score) for name, score in zip('first second third'.split(), '1 2 3'.split())]
>>> lst
[HighScores(name='first', score='1'), HighScores(name='second', score='2'), HighScores(name='third', score='3')]
>>> lst[0].name
'first'
>>> max(lst, key=lambda x: x.score).name    # name of the player with max score
'third'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Ok, thank you for the help
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  azure TTS from text files to mp3s mutantGOD 2 1,706 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
  Writing into 2 text files from the same function paul18fr 4 1,682 Jul-28-2022, 04:34 AM
Last Post: ndc85430
  Delete empty text files [SOLVED] AlphaInc 5 1,569 Jul-09-2022, 02:15 PM
Last Post: DeaD_EyE
  select files such as text file RolanRoll 2 1,175 Jun-25-2022, 08:07 PM
Last Post: RolanRoll
  Two text files, want to add a column value zxcv101 8 1,935 Jun-20-2022, 03:06 PM
Last Post: deanhystad
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,522 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Separate text files and convert into csv marfer 6 2,882 Dec-10-2021, 12:09 PM
Last Post: marfer
  Sorting and Merging text-files [SOLVED] AlphaInc 10 4,903 Aug-20-2021, 05:42 PM
Last Post: snippsat
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,153 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Several pdf files to text mfernandes 10 5,840 Jul-07-2021, 11:39 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020