Python-从函数中的文本文件中读取数字

Python-从函数中的文本文件中读取数字,python,file-io,Python,File Io,我正在尝试编写一个程序,确保在文本文件中查找给定数字和极限之间的素数。如果该数字存在于文件中,它将尝试写入另一个与给定(输入的)数字互质的数字。然后将其写入文本文件我的问题是检查文本文件中的数字是否存在。我该怎么写呢?所以,我从早上就开始研究了,但是,我找不到对这个问题有用的答案。我认为Python与C语言相比有很大的不同 示例:输入的数字为12,限制为3 生成的数字为1,5,7 文本文件上存在第二次运行的1,5,7,然后生成11,13,17并打印它们 def coprime(x,y):

我正在尝试编写一个程序,确保在文本文件中查找给定数字和极限之间的素数。如果该数字存在于文件中,它将尝试写入另一个与给定(输入的)数字互质的数字。然后将其写入文本文件我的问题是检查文本文件中的数字是否存在。我该怎么写呢?所以,我从早上就开始研究了,但是,我找不到对这个问题有用的答案。我认为Python与C语言相比有很大的不同

示例:输入的数字为12,限制为3

生成的数字为1,5,7

文本文件上存在第二次运行的1,5,7,然后生成11,13,17并打印它们

def coprime(x,y):
    """Returns True if the number is copime
    else False."""
    if x % y == 0:
        return False
    else:
        return True


"""

def text_file() function will be here
if number searching number exists on the text file return True
else return False


"""

f = open("numbers.txt","a+")
i = 0
j = 0

num = int(raw_input("Please enter number "))
limit = int(raw_input("Please enter limit "))

while i < limit:
    if text_check(file,j) == False and coprime(num,j) == True:
        f.write(str(j))
        i += 1
        j += 1
        print "%d is written on the text file" % j 

    else:
        j += 1

f.close()
def互质(x,y):
“”“如果数字为copime,则返回True
否则为假
如果x%y==0:
返回错误
其他:
返回真值
"""
def text_file()函数将出现在此处
如果文本文件上存在编号搜索编号,则返回True
否则返回False
"""
f=打开(“numbers.txt”,“a+”)
i=0
j=0
num=int(原始输入(“请输入数字”))
限制=整数(原始输入(“请输入限制”))
而我认为:
如果text_check(file,j)=False,互质(num,j)=True:
f、 写(str(j))
i+=1
j+=1
打印“%d”写入文本文件“%j”
其他:
j+=1
f、 关闭()

假设所有NUM都位于单独的行中,如下所示:

1
5
7



from fractions import gcd

def coprime(n1, n2):
    return gcd(n1, n2) == 1 # same as if gcd(n1, n2) == 1:return True else: return False



with open("out.txt","a+") as f: # with automatically closes your files
    # add all previous numbers into a set, casting to int using map 
    # map(int, f)  equivalent to [int(ele) for ele in f] in python2
    nums_set = set(map(int, f)) # with 1 5 and 7 in the file nums_set = set([1, 5, 7])
    i = 0
    j = 0

    num = int(raw_input("Please enter number "))
    limit = int(raw_input("Please enter limit "))

    while i < limit:
        if j not in nums_set and coprime(num,j):
            f.write("{}\n".format(j))
            print "{} is written on the text file.".format(j)
            i += 1
            j += 1
            # add current j's from this run to the set to avoid reopening and rechecking he file        
            nums_set.add(j)       
        else:
            j += 1
1
5.
7.
从分数导入gcd
def互质(n1,n2):
返回gcd(n1,n2)==1#与gcd(n1,n2)==1时相同:返回真;否则:返回假
将“out.txt”、“a+”)作为f:#with自动关闭文件
#将之前的所有数字添加到一个集合中,使用map强制转换为int
#映射(int,f)等价于python2中的[int(ele)for ele in f]
nums_set=set(映射(int,f))#文件nums_set=set([1,5,7])中有1 5和7
i=0
j=0
num=int(原始输入(“请输入数字”))
限制=整数(原始输入(“请输入限制”))
而我认为:
如果j不在nums_集合和互质(num,j)中:
f、 写入(“{}\n”.format(j))
打印“{}写在文本文件上。”.format(j)
i+=1
j+=1
#将此运行中的当前j添加到集合中,以避免重新打开和重新检查文件
nums_集合。添加(j)
其他:
j+=1

您需要将
a+
放在引号
'a+'
中。现在有什么问题?你是如何将数字写入文件的?使用
file\u write(str(j))
。我读到数字不能直接写出来。必须使用
str()
将它们转换为字符串?我错了吗@PadraicCunningham@PadraicCunningham不是开玩笑,而是检查一下这个坏主意:使用名称
file
,您正在重新定义一个内置(
file
是Python 2中
open
的别名)。就我个人而言,我发现Python比C更简单、更符合逻辑,而且我从20世纪80年代初就开始用C编写代码。谢谢。但是,我想学地图吗?我现在有一些关于set的信息。
map(int,f)
相当于
[int(ele)表示f中的ele]