Python Forum
Python output into .txt or .csv file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python output into .txt or .csv file
#1
My current Code is below. I am attempting to take my outputs from this code and place it into a .txt or .csv file.
When I run this, I am getting the error "write() argument must be str, not int" -- does anyone have any suggestions on what I need to fix to make this work? have been struggling with it for a few days but cant seem to figure out the issue. Thank you in advance for your help!

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

#print(find_maxPrimes())

def find_maxPrimes2():
#    max = 0
#    maxNum = 0
#Printing here directly
    for i in range (1000, 10000):
        print (i, " ---> ", getNumPrimes(i))       
#print(find_maxPrimes2())

fh = open("numperms","a")
fh.write(find_maxPrimes())
fh.write(find_maxPrimes2())
fh.close()
Reply
#2
Well, find_maxPrimes returns an int. You need to convert that to a str before you can write it to a file, like the error says. You can do this with the str() built-in function.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Jul-17-2018, 04:46 PM)ichabod801 Wrote: Well, find_maxPrimes returns an int. You need to convert that to a str before you can write it to a file, like the error says. You can do this with the str() built-in function.

Thank you for your help! How do I do that without changing a lot of the code that I already have?
I have tried to insert the str function but I guess I am placing it in the wrong line??
looking for some guidance on where to insert the str function. I have been trying to figure this out for a few days...
Reply
#4
fh.write(str(find_maxPrimes()))
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
Photo how I write the output into xml file in python? 3lnyn0 1 2,166 Oct-31-2021, 05:40 PM
Last Post: Gribouillis
  Showing and saving the output of a python file run through bash Rim 3 2,372 Oct-06-2021, 10:48 AM
Last Post: gerpark
  Output CSV file with filepath and file contents glittergirl 1 1,710 Aug-03-2020, 01:50 AM
Last Post: glittergirl
  Python interpreter - output to file SectionProperties 8 5,403 Apr-11-2020, 01:38 PM
Last Post: SectionProperties
  csv file output rturus 7 3,162 Jan-30-2020, 02:09 PM
Last Post: buran
  File name as part of output file name Jeeping_Coder 1 2,081 Jan-10-2020, 03:43 PM
Last Post: Clunk_Head
  How to extract a matrix from .xml.gz file to a excel file or any other output? enyrb 0 2,030 Oct-21-2019, 01:01 PM
Last Post: enyrb
  python file output to log file Rsh 4 3,643 Aug-13-2019, 09:00 PM
Last Post: DeaD_EyE
  Output SQL to csv or xls file? JP_ROMANO 4 2,544 Aug-02-2019, 01:58 AM
Last Post: JP_ROMANO

Forum Jump:

User Panel Messages

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