Hi so in my list i have multiple characters - '32' which i need to replace with a space, would anybody be able to give an example of how to
do this? Thanks

Edited 1 Year Ago by joshuawilson11

It still hasn't worked, here's the output

[83, 111, 109, 101, 119, 104, 101, 114, 101, 32, 105, 110, 32, 108, 97, 32, 77, 97, 110, 99, 104, 97, 44, 32, 105, 110, 32, 97, 32, 112, 108, 97, 99, 101, 32, 119, 104, 111, 115, 101, 32, 110, 97, 109, 101, 32, 73, 32, 100, 111, 32, 110, 111, 116, 32, 99, 97, 114, 101, 32, 116, 111, 32, 114, 101, 109, 101, 109, 98, 101, 114, 44, 32, 97, 32, 103, 101, 110, 116, 108, 101, 109, 97, 110, 32, 108, 105, 118, 101, 100, 32, 110, 111, 116, 32, 108, 111, 110, 103, 32, 97, 103, 111, 44, 32, 111, 110, 101, 32, 111, 102, 32, 116, 104, 111, 115, 101, 32, 119, 104, 111, 32, 104, 97, 115, 32, 97, 32, 108, 97, 110, 99, 101, 32, 97, 110, 100, 32, 97, 110, 99, 105, 101, 110, 116, 32, 115, 104, 105, 101, 108, 100, 32, 111, 110, 32, 97, 32, 115, 104, 101, 108, 102, 32, 97, 110, 100, 32, 107, 101, 101, 112, 115, 32, 97, 32, 115, 107, 105, 110, 110, 121, 32, 110, 97, 103, 32, 97, 110, 100, 32, 97, 32, 103, 114, 101, 121, 104, 111, 117, 110, 100, 32, 102, 111, 114, 32, 114, 97, 99, 105, 110, 103, 46]
Here's the code for reference,
thanks

import random
#=============================================Main Menu=============================================
print("Hi, Welcome to Text Encyrption")
print("This program will encrypt or decrypt a text file chosen by you.") 
print('Make a choice')
print('1. Encrypt Message')
print('2. Decrypt Message ')
print('3. Exit program ')
print()
#the user only has 3 choices
menu_choice = 0
#menu_choice cannot = 3
while menu_choice != 3:
    menu_choice = int(input("Choose an option (1-3): "))
#=====================================Encryption=======================================================
    if menu_choice == 1:
        print("ENCRYPTING MESSAGE")
        print("For this task the file is Text File1.txt")
        text_file = input(str("Enter the file you want to be Encyrpted"))
        ciphertext_file = input("Enter the file to write the encrypted message out to: ")
#this will find the text file the user has inputter
        apple = open(text_file,"r")
#this puts the text file so it can only be read
        apple =(apple.read())
        print(apple)
        n = 0
        number = []
        texting = []
#================================Offset Factor=================================================== 
#while n is less than 8
        while n<8:
#it will convert a random number between 33 and 126 into ascii
            random_number = chr(random.randint(33, 126))
            print(random_number)
#every time n + 1
#one turn out of 8
            n = n + 1
#the number is put into ascii
            characters = ord(random_number)
#it is put into the number list
            number.append(characters)
        print(number)
#this rounds each of the eight integers in number and divides by 8 and takes away 3
        offset_number = round((number[0]+number[1]+number[2]+number[3]+number[4]+number[5]+number[7])/8)-32
#this prints the offset factor
        print(offset_number)
#=============================Task 5=====================================================================
        newString = []
#this is the only part of ascii allowed
        validLetters = "#$%&'()*+,-./:;<=>?@[\]^_`{|}~""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghiklmnopqrstuvwxyz{|}~"
        space = [' ']
        new = []
        old = []
        x = 0
#this splits the text file into singukar characters from 0 to 1
        chrsplit = [apple[i:i+1] for i in range(0, len(apple),1)]
#this puts into ascii every character
        ascii_conversion = [ord(i) for i in chrsplit]
        print(ascii_conversion)
#this replaces each 32 in the list with a space
        ascii_conversion[:] = [(' ' if x == '32' else x) for x in ascii_conversion]

        print(ascii_conversion)

L[:] = [(' ' if x == 32 else x) for x in L]

although I would just remove the element instead of replacing it

Edited 1 Year Ago by Slavi

Hmm ...

import textwrap

mylist = [83, 111, 109, 101, 119, 104, 101, 114, 101, 32, 105,
110, 32, 108, 97, 32, 77, 97, 110, 99, 104, 97, 44, 32, 105,
110, 32, 97, 32, 112, 108, 97, 99, 101, 32, 119, 104, 111, 115,
101, 32, 110, 97, 109, 101, 32, 73, 32, 100, 111, 32, 110, 111,
116, 32, 99, 97, 114, 101, 32, 116, 111, 32, 114, 101, 109, 101,
109, 98, 101, 114, 44, 32, 97, 32, 103, 101, 110, 116, 108, 101,
109, 97, 110, 32, 108, 105, 118, 101, 100, 32, 110, 111, 116, 32,
108, 111, 110, 103, 32, 97, 103, 111, 44, 32, 111, 110, 101, 32,
111, 102, 32, 116, 104, 111, 115, 101, 32, 119, 104, 111, 32, 104,
97, 115, 32, 97, 32, 108, 97, 110, 99, 101, 32, 97, 110, 100, 32,
97, 110, 99, 105, 101, 110, 116, 32, 115, 104, 105, 101, 108, 100,
32, 111, 110, 32, 97, 32, 115, 104, 101, 108, 102, 32, 97, 110, 100,
32, 107, 101, 101, 112, 115, 32, 97, 32, 115, 107, 105, 110, 110,
121, 32, 110, 97, 103, 32, 97, 110, 100, 32, 97, 32, 103, 114, 101,
121, 104, 111, 117, 110, 100, 32, 102, 111, 114, 32, 114, 97, 99, 105,
110, 103, 46]

mystr = "".join(chr(n) for n in mylist)
print(textwrap.fill(mystr, 60))

''' result ...
Somewhere in la Mancha, in a place whose name I do not care
to remember, a gentleman lived not long ago, one of those
who has a lance and ancient shield on a shelf and keeps a
skinny nag and a greyhound for racing.
'''

Not sure what you want to replace with what.

Edited 1 Year Ago by vegaseat

This article has been dead for over six months. Start a new discussion instead.