删除每一行中的字符串

时间:2016-11-12 09:07:56

标签: python

我有这些输入字符串:

omarReturn
waelReturn

我希望输出字符串为:

omar
wael

这是我的代码

 write = file("e", 'a')
    write.write(event.Key)

    if event.Key=="Return":
        read = file("e", 'r')
        lines = read.readlines()
        read.close()
        # How do I remove "Return" from the lines
        write.write("\n")

3 个答案:

答案 0 :(得分:0)

这将从字符串“a”中删除每个“Return”。

所以迭代到你的每一行并使用这个命令,而改变变量应该工作

a=a.replace("Return","")

答案 1 :(得分:0)

您可以使用re.sub()。如果它出现在最后,这将删除package rservedem; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; import org.rosuda.REngine.*; import org.rosuda.REngine.Rserve.*; /** * * @author Carl */ public class RserveDem { /** * @param args the command line arguments */ public static void main(String[] args) { RConnection connRserve; Scanner inputStream = null; try { inputStream = new Scanner(new FileInputStream("C:/vikt-fore_data2Update.csv")); //läser in data från fil } catch (FileNotFoundException e) { System.out.println("Filen hittades inte! Stänger av..."); System.exit(0); } inputStream.useDelimiter(","); int vikt; double [] memberArray = new double[157]; for (vikt = 0; vikt < memberArray.length; vikt++) { memberArray [vikt] = Double.valueOf(inputStream.next()); } try { connRserve = new RConnection(); connRserve.assign("memberArr", memberArray); String toRserveForProcessing = "t.test(memberArr);"; System.out.println("Den skapade strängen som skickas till Rserve " + toRserveForProcessing); String summan = connRserve.eval(toRserveForProcessing).asString(); System.out.println ("Svaret från rserve " + summan); } catch(RserveException ex) { System.out.println(ex.getMessage()); } catch (REXPMismatchException ex) { System.out.println(ex.getMessage()); } catch (Exception e) { System.out.println("Seomthing went wrong, but it's not Rserve: " +e.getMessage()); e.printStackTrace(); } } } 子字符串,否则不会。

让我们创建一个函数attempt to access org.rosuda.REngine.REXPGenericVector as String

"Return"

输出:

substring_remover()

因此,确切的代码将是:

import re 
def substring_remover(whole_string):
    return re.sub(r'(.+)(Return)$',r'\1',whole_string)      

print substring_remover('omarReturn') # Will remove "Return" as it is at the end
print substring_remover('omarNormal') # Will remove nothing as there is no "Return"
print substring_remover('omarReturnExtra') # Will remove nothing because "Return" exists but not at the end. 

答案 2 :(得分:0)

假设输入数据位于与Python代码相同的文件夹中名为“inputfile.txt”的文件中,并且以UTF-8编码。

# Open the input file for reading.
with open('inputfile.txt', mode='r', encoding='utf-8') as infile:
    # Store all the data from the input file in a variable named
    #  'inlines'.  This only makes sense if you have a small amount
    #  of data, as you suggest you do.
    inlines = infile.readlines()

# Open or create a file named 'outputfile.txt' in the same folder as
#  'inputfile.txt' with UTF-8 encoding.
with open('outputfile.txt', mode='w', encoding='utf-8') as outfile:
    # Walk each line in the input file.
    for line in inlines:
        # Write each input line to the output file with 'Return'
        #  removed.
        outfile.write(line.replace('Return', ''))