The Issue/Question Details
I have a file (blah.txt) where its contents (a list) look like this:
Key1 = ['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',' ']
Inside of a simple python file (called CAT.py) I execute the following commands:
infile = open('blah.txt', 'r')
Key1 = infile.readline()
infile.close()
Key1 = Key1.rstrip('\n')
print(Key1)
Key1 = ['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',' ']
print(''.join(Key1))
Key1 = ['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',' '] <---- THIS IS WHAT I DO NOT WANT
However….
If in a separate simple python file (called DOG.py) I execute the following commands on a list:
Key1 = ['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',' ']
print(Key1)
['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', ' ']
print(''.join(Key1))
ABCDEFGHIJKLMNOPQRSTUVWXYZ <---- THIS IS WHAT I WANT
The Issue/Question
I ultimately want the output that looks like “ABCDEFGHIJKLMNOPQRSTUVWXYZ”, but for the life of me I cannot figure out how to read in a list for a file and then convert that list to a single string of characters. Can someone tell me where I am going wrong and also show me some code that would fix my problem?
You have to transform the string
to a list
before you can apply join
.
First we use partition
to get the part after the =
. Then ast.literal_eval
will create a list out of the string.
import ast
line = "Key1 = ['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',' ']"
data = line.partition('=')[2].strip()
elements = ast.literal_eval(data)
print(''.join(elements))
Additional: You could use with
in the context of opening the file. If you do it, Python will close the file for you.
with open('blah.txt', 'r') as infile:
line = infile.readline()
As jamylak pointed out: If you control the file please don't store the data like this. As you can see it's not easy to handle. You could store and read the data as JSON.
infile = open('blah.txt', 'r')
Key1 = infile.read()
import re
y=re.findall(r"([a-zA-Z])",Key1.split("=")[1])
print "".join(y)
You can simply try this.
>>> infile = open('blah.txt', 'r')
>>> key1 = infile.readline()
>>> infile.close()
>>> key1 = "Key1 = ['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',' ']"
>>> key1 = key1[key1.index('['):key1.index(']')+1]
>>> key1
"['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',' ']"
>>> ''.join(eval(key1))
'ABCDEFGHIJKLMNOPQRSTUVWXYZ '
use ast.literal_eval
.
import ast
with open('blah.txt') as f:
for line in f: # line = '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',' '.
print ''.join(ast.literal_eval(line))
use str.split
and split by =
and then select the list of characters by indexing [-1]
will gives you last index which carry the list of characters and then use ast.literal_eval
to get the character and then use str.join
to join all the characters.
import ast
with open('blah.txt') as f:
for line in f: # if line = key1 = ['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',' ']
print ''.join(ast.literal_eval(line.split(' = ')[-1]))
YOur problem is that in your first example key1 is a string and not a list. The following should give you the desired output:
str_input = "Key1 = ['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',' ']"
remove_unnecessary_data = str_input[str_input.find('[')+1: str_input.find(']')]
answer = ''.join([char for char in remove_unnecessary_data if char not in("'", ",")])
import re
string = ""
read = open('blah.txt', 'r')
key = read.readline()
a = re.compile("[AZ]")
for char in key:
if a.match(char):
string = string + char
print string
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.