Python Forum
Dictionary to .txt or .csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dictionary to .txt or .csv file
#9
Hello Everyone,

I have been struggling with this assignment for quite sometime. I am attempting to take my code with the dictionary created but I need to take the dictionary data and place it into a .txt file or .csv file so I can use it in an excel document to further investigate the data created from the code. The code is as follows:

import pickle


def getAllCombinations(s, length):
    if length == 0:
        return ['']
    else:
        ret = []
        for idx, c in enumerate(s):
            combos = getAllCombinations(s[0:idx] + s[idx+1:], length - 1)
            for i in range(len(combos)):
                combos[i] = c + combos[i]
            ret += combos
        return ret
 
def getPermutations(s, ret, swapIdx = 0):
    if swapIdx == len(s):
        ret.append(int(''.join(s)))
 
    for i in range(swapIdx, len(s)):
        cpy = [c for c in s]
        cpy[swapIdx], cpy[i] = cpy[i], cpy[swapIdx]
        getPermutations(cpy, ret, swapIdx + 1)
 
def getAllPermutations(i):
    s = str(i)
    allPerms = set()
    for i in range(len(s)):
        curCombos = getAllCombinations(s, i + 1)
        for combo in curCombos:
            ret = []
            getPermutations(combo, ret)
            allPerms = allPerms.union(set(ret))
    return list(allPerms)
 
def isPrime(n):
    if n==2 or n==3: return True
    if n%2==0 or n<2: return False
    for i in range(3,int(n**0.5)+1,2):   # only odd numbers
        if n%i==0:
            return False    
    return True

def getNumPrimes(i):
    perms = getAllPermutations(i)
    numprimes = 0
    for perm in perms:
        if (isPrime(perm)):
            numprimes += 1
    return numprimes
 
 
def find_maxPrimes():
    max = 0
    maxNum = 0
    for i in range(1000, 10000):
        cur = getNumPrimes(i)
        if (max < cur):
            max = cur
            maxNum = i
    return (maxNum)


def make_dict():
#    max = 0
#    maxNum = 0
#Printing here directly
    Dict = {}
    for i in range(1000, 10000):
        Dict[i] = getNumPrimes(i)
    return Dict



def make_db(name):
    db = make_dict()
    pickle_temp = open(f'{name}', 'wb')
    pickle.dump(db,pickle_temp)
    pickle_temp.close()
    
def open_db(name):
    with open(f'{name}', 'rb') as pickle_temp:
        db = pickle.load(pickle_temp)
    return db

make_db('test_db_two')
I will also attatch my .py file

I appreciate any help I can get as I need to complete this program as soon as possible so I can write a paper about the coding.
Reply


Messages In This Thread
Dictionary to .txt or .csv file - by stanthaman42 - Aug-07-2018, 05:05 PM
RE: Dictionary to .txt or .csv file - by Vysero - Aug-08-2018, 03:37 PM
RE: Dictionary to .txt or .csv file - by Vysero - Aug-07-2018, 05:31 PM
RE: Dictionary to .txt or .csv file - by Vysero - Aug-07-2018, 05:45 PM
RE: Dictionary to .txt or .csv file - by Vysero - Aug-07-2018, 07:00 PM
RE: Dictionary to .txt or .csv file - by Vysero - Aug-07-2018, 08:33 PM
Dictionary to .txt or .csv file - by stanthaman42 - Aug-07-2018, 08:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  dictionary output to text file (beginner) Delg_Dankil 2 1,298 Jul-12-2023, 11:45 AM
Last Post: deanhystad
  Using dictionary to find the most sent emails from a file siliusu 6 7,730 Apr-22-2021, 06:07 PM
Last Post: siliusu
  Updating dictionary in another py file tommy_voet 1 5,031 Mar-28-2021, 07:25 PM
Last Post: buran
  Making a dictionary from a file instyabam 0 1,556 Oct-27-2020, 11:59 AM
Last Post: instyabam
  how can i create a dictionary of dictionaries from a file Astone 2 2,343 Oct-26-2020, 02:40 PM
Last Post: DeaD_EyE
  Convert all actions through functions, fill the dictionary from a file Astone 3 2,523 Oct-26-2020, 09:11 AM
Last Post: DeaD_EyE
  how to put text file in a dictionary infected400 2 3,048 Jan-06-2019, 04:43 PM
Last Post: micseydel
  dictionary from file Zatoichi 5 3,922 Feb-11-2018, 07:15 PM
Last Post: j.crater
  Dictionary + File Reading palmtrees 2 4,981 Nov-15-2016, 05:16 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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