Home Download Buy Blog Forum Support

I want a (.PY) script that only save specific text letters

I want a (.PY) script that only save specific text letters

Postby Jandazekon on Sun Jan 13, 2013 2:52 pm

This program takes input.txt
It has unwanted (1234567890) and so on.

It only saves these letters:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z a b c d e f g h i j k l m n o p q r s t u v w x y z

(space button)

(enter button) and (tab button) is converted to (space button)

Now collapse consecutive whitespaces into one space.

The others are removed.


Then the output.txt is created.
Jandazekon
 
Posts: 1
Joined: Sun Jan 13, 2013 2:50 pm

Re: I want a (.PY) script that only save specific text letters

Postby FichteFoll on Mon Jan 21, 2013 9:50 pm

Try this, untested and inefficient due to re but it should work:

Code: Select all
import re

with open("input.txt", "r") as f:
    txt = f.read()

txt = re.sub(r"[^A-Za-z]", '', txt)
txt = re.sub(r"\s+", " ", txt)

with open("output.txt", "w") as f:
    txt = f.write()
FichteFoll
 
Posts: 400
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany


Return to Ideas and Feature Requests

Who is online

Users browsing this forum: Exabot [Bot] and 5 guests