Find words with given suffix in file and remove prefix

This is the place for queries that don't fit in any of the other categories.

Find words with given suffix in file and remove prefix

Postby zvonko92 » Wed Jul 30, 2014 12:06 pm

I'm trying to create a simple program that finds all words in given file, words which have one of given suffixes.
Second part of task is to remove prefix, if any, form word and print it out.

Here is my code:
Code: Select all
filename=raw_input("Insert name of file")
suffixes=['ing','an', 'er', 'x']
prefixes=['za', 're'],

words = open("%s"%filename).read().split()

for i in words:
    """if word contains one of given suffixes(don't know how to do this lines):
            If word contanis one of given prefixes:
                remove prefix
                print i
           else:
               print i"""


Can you help me make line in quotes? Thanks in advance.
Last edited by metulburr on Wed Jul 30, 2014 4:05 pm, edited 3 times in total.
Reason: locked post
zvonko92
 
Posts: 8
Joined: Mon Jul 28, 2014 4:13 pm

Re: find words with given suffix in file

Postby DrakeMagi » Wed Jul 30, 2014 12:10 pm

Code: Select all
for suff in suffixes:
    if i.endswith(suff):
        print i


better yet
Code: Select all
if i.endswith(tuple(suffixes)):
    print i
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 113
Joined: Sun May 12, 2013 8:36 pm

Re: Find words with given suffix in file and remove prefix

Postby DrakeMagi » Wed Jul 30, 2014 2:19 pm

zvonko92 wrote:I'm trying to create a simple program that finds all words in given file, words which have one of given suffixes.
Second part of task is to remove prefix, if any, form word and print it out.

Here is my code:
Code: Select all
filename=raw_input("Insert name of file")
suffixes=['ing','an', 'er', 'x']
prefixes=['za', 're'],

words = open("%s"%filename).read().split()

for i in words:
    """if word contains one of given suffixes(don't know how to do this lines):
            If word contanis one of given prefixes:
                remove prefix
                print i
           else:
               print i"""


Can you help me make line in quotes? Thanks in advance.

Since you change your post i qoute you this time.

edit to work with prefixes
1. take my first example.
Code: Select all
for prefix in prefixes:
    if i.startswith(prefix):
        .. rest up to you

2. value = len(prefix)
3. slice the string
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 113
Joined: Sun May 12, 2013 8:36 pm


Return to General Coding Help

Who is online

Users browsing this forum: mdovey and 5 guests