dimanche 12 mai 2019

How to print first item in a string from a dictionary with a random number and print remaining numbers?

I am creating a function that opens and reads a text file, separated by commas. The function will create a dictionary of the text file and print out the first item in each string, along with a random number (1-10) as the key. The values will then be the remaining numbers in their respective string

Ex. of file

  January,900,483,394,883,649
  February,200,329,596,283,972
  March,120,237,923,102,787

I have been able to get the dictionary printed, but the output is nowhere near what I want it to be.

import random
def dictionaryTest():
  openfile= open('example.txt', 'r')
  monthData = openfile.readlines()
  openfile.close


  dictionary = {}
  rNum = random.randrange(1,11)

  for lines in monthData:
      info = line.split()
      key, values = info[0], info[1:]
      d[key] = info[0], rNum
      values = info[1:]

  print(dictionary)

Expected Output:

{('January, 8'): [900,483,394,883,649],('February, 1'): 
[200,329,596,283,972],('March, 4'): [120,237,923,102,787]}

Actual Output:

{'January, 900, 483, 395, 883, 649':('January, 900, 483, 395, 883, 
649'), 'February, 200,329,596,283,972':('February, 
200,329,596,283,972'), 'March, 120,237,923,102,787':('March, 
120,237,923,102,787')}




Aucun commentaire:

Enregistrer un commentaire