简体   繁体   中英

how to convert text file to dictionary in python

I need to convert lines of different lengths to one dictionary. It's for player stats. The text file is formatted like below. I need to return a dictionary with each player's stats.

{Lebron James:(25,7,1),(34,5,6), Stephen Curry: (25,7,1),(34,5,6), Draymond Green: (25,7,1),(34,5,6)}

Data:

Lebron James

25,7,1

34,5,6

Stephen Curry

25,7,1

34,5,6

Draymond Green

25,7,1

34,5,6

I need help starting the code. So far I have a code that removes the blank lines and makes the lines into a list.

myfile = open("stats.txt","r") 
for line in myfile.readlines():  
    if line.rstrip():
         line = line.replace(",","")       
         line = line.split()

I think this should do what you want:

data = {}
with open("myfile.txt","r") as f:
    for line in f:
        # Skip empty lines
        line = line.rstrip()
        if len(line) == 0: continue
        toks = line.split(",")
        if len(toks) == 1:
            # New player, assumed to have no commas in name
            player = toks[0]
            data[player] = []
        elif len(toks) == 3:
            data[player].append(tuple([int(tok) for tok in toks]))
        else: raise ValueErorr # or something

The format is somewhat ambiguous, so we have to make some assumptions about what the names can be. I've assumed that names can't contain commas here, but you could relax that a bit if needed by trying to parse int,int,int, and falling back on treating it as a name if it fails to parse.

Here's a simple way to do this:

scores = {}

with open('stats.txt', 'r') as infile:

    i = 0

    for line in infile.readlines():

        if line.rstrip():

             if i%3!=0:

                 t = tuple(int(n) for n in line.split(","))
                 j = j+1

                 if j==1:
                    score1 = t # save for the next step

                 if j==2:
                    score  = (score1,t) # finalize tuple

              scores.update({name:score}) # add to dictionary

         else:

            name = line[0:-1] # trim \n and save the key
            j = 0 # start over

         i=i+1 #increase counter

print scores

Maybe something like this:

For Python 2.x

myfile = open("stats.txt","r") 

lines = filter(None, (line.rstrip() for line in myfile))
dictionary = dict(zip(lines[0::3], zip(lines[1::3], lines[2::3])))

For Python 3.x

myfile = open("stats.txt","r") 

lines = list(filter(None, (line.rstrip() for line in myfile)))
dictionary = dict(zip(lines[0::3], zip(lines[1::3], lines[2::3])))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM