Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从一个递归文件中选择单词,并在另一个文件python中打印出来_Python - Fatal编程技术网

从一个递归文件中选择单词,并在另一个文件python中打印出来

从一个递归文件中选择单词,并在另一个文件python中打印出来,python,Python,从文件(stringsToTest.txt)中获取输入,并仅输出 另一个文件的回文(stringsArePalindromes.txt)。忽略空格、大写和标点符号 确定字符串是否为回文时,如果是回文,则将原始字符串写入文件 这就是我到目前为止所做的: def ispalindrome(text): file=open(str(text),"r") a=file.readlines() #f=open(str("stringsarepalindromes.txt"),"w"

从文件(stringsToTest.txt)中获取输入,并仅输出 另一个文件的回文(stringsArePalindromes.txt)。忽略空格、大写和标点符号

确定字符串是否为回文时,如果是回文,则将原始字符串写入文件

这就是我到目前为止所做的:

def ispalindrome(text):
    file=open(str(text),"r")
    a=file.readlines()
    #f=open(str("stringsarepalindromes.txt"),"w")
    for x in a:
        nomorepunc=x.replace("!","")
        nopunc=nomorepunc.replace(".","")
        nospace=nopunc.replace(" ","")
        samecase=(nospace.lower())
        morecase=samecase.replace("?","")
        evencase=morecase.replace("'","")
        cases=evencase.replace(":","")
        #print(cases)
        words=str(cases)
        c=words.split()


        if len(c) < 2:

            print(c,"true")
            #f.write(y)

        if c[0]!= c[-1]:
            print("no")



        return ispalindrome(c[1:-1])
    #open("stringsarepalindromes.txt")"""
def ispalindrome(文本):
文件=打开(str(文本),“r”)
a=文件。readlines()
#f=打开(str(“stringsarepalindromes.txt”),“w”)
对于a中的x:
nomorepunc=x.replace(“!”,“”)
nopunc=nomorepunc.replace(“.”,“”)
nospace=nopunc.replace(“,”)
samecase=(nospace.lower())
morecase=samecase.replace(“?”,“”)
evencase=morecase.replace(“”,“”)
cases=evencase.replace(“:”,“”)
#印刷品(箱)
words=str(例)
c=单词。拆分()
如果len(c)<2:
打印(c,“真实”)
#f、 写入(y)
如果c[0]!=c[-1]:
打印(“否”)
返回ispalindrome(c[1:-1])
#打开(“stringsarepalindromes.txt”)“

要删除所有标点符号:

remove = "!.?':,"   # put all characters you want to remove in here
要删除这些字符,并降低字符串中的所有字母,可以说(其中
x
是字符串)

之后,您可以通过简单地检查反向字符串来测试回文

x == x[::-1]

这将删除所有标点符号并将回文写入新文件:

import string
def ispalindrome(text):
    with open(text) as f,open("output.txt","w") as f1: # use with to open your files to close them automatically
        for line in f:
            test = line.strip().lower().translate(string.maketrans("",""), string.punctuation).replace(" ","") # remove punctuation and whitespace
            if test[::-1] == test: # if the line and line reversed are equal, we have a palindrome
                f1.write(line) # write original line to outfile.txt
            print "Is {} a palindrome? {}".format(test,test[::-1] == test) # just a line to show you what is happening

到目前为止,您的问题到底是什么?错误(提供完整的回溯)?意外的输出(提供输入以及预期和实际的输出)?您忘记问一个问题。这里有一个提示:
def is_palindrome(a):返回a==a[:-1]
当我运行代码时,它只运行文件的第一行。我应该如何将return放入循环中而不使其在第一行之后停止?或者我可以完全消除forloop吗?将文件从循环中读取。然后你可以在循环中逐行调用
def isPallendrome()
函数
import string
def ispalindrome(text):
    with open(text) as f,open("output.txt","w") as f1: # use with to open your files to close them automatically
        for line in f:
            test = line.strip().lower().translate(string.maketrans("",""), string.punctuation).replace(" ","") # remove punctuation and whitespace
            if test[::-1] == test: # if the line and line reversed are equal, we have a palindrome
                f1.write(line) # write original line to outfile.txt
            print "Is {} a palindrome? {}".format(test,test[::-1] == test) # just a line to show you what is happening