pythonsortingtxt

appending string to txt file only appends first letter python


with open("words_alpha.txt", 'r') as f:
  your_result = [line.split(',') for line in f.read().splitlines()]

for i in your_result:
  for word in i:
    x = len(word)
    print(word)
    if x == 1:
      with open("1.txt", "a") as myfile:
        myfile.write(word)
    if x == 2:
      with open("2.txt", "a") as myfile:
        myfile.write(word)

    if x == 3:
      with open("3.txt", "a") as myfile:
        myfile.write(word)

    if x == 4:
      with open("4.txt", "a") as myfile:
        myfile.write(word)
    if x == 5:
      with open("5.txt", "a") as myfile:
        myfile.write(word)

    if x == 6:
      with open("6.txt", "a") as myfile:
        myfile.write(word)

    if x == 7:
      with open("7.txt", "a") as myfile:
        myfile.write(word)
    if x == 8:
      with open("8.txt", "a") as myfile:
        myfile.write(word)

    if x == 9:
      with open("9.txt", "a") as myfile:
        myfile.write(word)

    if x == 10:
      with open("10.txt", "a") as myfile:
        myfile.write(word)
    if x == 11:
      with open("11.txt", "a") as myfile:
        myfile.write(word)

    if x == 12:
      with open("12.txt", "a") as myfile:
        myfile.write(word)r 

the code prints the word from a wordlist correctly, but when the same variable is appended, it only adds the first letter to each file. the code outputs the words line by line, but when appended, only shows up as a bunch of letters. the alpha words txt file is a wordlist with words on every line. i want to sort the words by letter count


Solution

  • I would define a function for this task: If your words_alpha.txt looks like:

    Alpha
    Beta
    Gamma
    …
    
    with open("words_alpha.txt", 'r') as f:
      your_result = [line.split(',') for line in f.read().splitlines()]
    
    def append_file(word, length):
        with open(f"{length}.txt", "a") as myfile:
            myfile.write(f"\n{word}")
            print(f"word: {word} with length {length} written in {length}.txt")
                
    for word in your_result:
        append_file(str(*word), int(len(*word)))
    

    If you have a comma separated list, like: Alpha, Beta, Gamma, … , one the same row, than this code should be used:

    
    with open("words_alpha.txt", 'r') as f:
        your_result = [line.split(',') for line in f.read().splitlines()]
    
    def append_file(word, length):
        with open(f"{length}.txt", "a") as myfile:
            myfile.write(word+'\n')
            print(f"word: {word} with length {length} written in {length}.txt")
                
    for row in your_result:
        for word in row:
            append_file(word.lstrip(), len(word.lstrip()))
    
    

    The output files n.txt will write each word in a new line.

    For a comma separated list ( csv) use better the csv reader():

    import csv
    
    def append_file(word, length):
        with open(f"{length}.txt", "a") as myfile:
            myfile.write(word+'\n')
            print(f"word: {word} with length {length} written in {length}.txt")
    
    with open("words_alpha.txt", 'r') as csvfile:
        cr = csv.reader(csvfile, delimiter=' ')
        for row in cr:
            for word in row:
                append_file(word.strip(','), len(word.strip(',')))