Python Forum
Grouping lots of words in lots of txt files: programmer needed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grouping lots of words in lots of txt files: programmer needed
#11
Is this your homework?


#!/usr/bin/env python3
from string import ascii_uppercase as upper

# creating a mapping for A-Z : 1-26
# access is very fast
mapping = {char: value for (char, value) in zip(upper, range(1, len(upper) + 1))}

# {'A': 1,
#  'B': 2,
#  'C': 3,
#  'D': 4,
#  'E': 5,
#  'F': 6,
#  'G': 7,
#  'H': 8,
#  'I': 9,
#  'J': 10,
#  'K': 11,
#  'L': 12,
#  'M': 13,
#  'N': 14,
#  'O': 15,
#  'P': 16,
#  'Q': 17,
#  'R': 18,
#  'S': 19,
#  'T': 20,
#  'U': 21,
#  'V': 22,
#  'W': 23,
#  'X': 24,
#  'Y': 25,
#  'Z': 26}

# functional approach
def word_sum(word):
    return sum(map(lambda x: mapping.get(x.upper(), 0), word))

# word_sum('!-.$%"§A')
# should return 1

# better to understand
def word_sum(word):
    result = 0
    for char in word:
        # make char uppercase, return 0 if char not in mapping
        # https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
        value = mapping.get(char.upper(), 0)
        result += value
    return result


def analyse_and_write(file_in):
    # using a context manager
    # leaving the context closes the file :-)
    with open(file_in) as fd:
        # this iterates line for line over the text.
        for line in fd:
            # calculates the sum and converts the integer into a string
            s = word_sum(line)
            if not s: #when s == 0, skip it
                continue
            s = str(s) # we need the number as string to write it into the textfile
            file_out = s + '.txt'
            # here a context manager again to write the file
            with open(file_out, 'a') as out:
            # 'a' is for append. If the file does not exists,
            # it will be created. If the file exists, it will append.
                out.write(line)


if __name__ == '__main__':
    print('Staring program')
    analyse_and_write('WordList100000Plus.txt')
    print('Done')
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Grouping lots of words in lots of txt files: programmer needed - by DeaD_EyE - May-22-2017, 02:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Programmer looking for work Johndg20 1 2,394 Sep-12-2021, 01:21 PM
Last Post: Larz60+
  programmer for my project siliconvalleymusicbrokerscom 1 3,041 Feb-01-2018, 07:51 AM
Last Post: PythonMagic003
  Python programmer needed to integrate GUI in Linux/LinuxCNC blazini36 1 4,361 Aug-18-2017, 03:32 PM
Last Post: victor_cis
  Need hired help from python programmer oj43085 6 6,528 May-10-2017, 06:06 PM
Last Post: nilamo
  Summer programmer needed to teach camp Tampa, Florida MOSI MOSI 0 3,156 Apr-18-2017, 04:55 PM
Last Post: MOSI

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020