简体   繁体   English

Python - 打开文件 - readline - list - 转换为字符串

[英]Python - open file - readline - list - convert to string

The Issue/Question Details 问题/问题详情

I have a file (blah.txt) where its contents (a list) look like this: 我有一个文件(blah.txt),其内容(列表)如下所示:

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 = ['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: 在一个简单的python文件(称为CAT.py)中,我执行以下命令:

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',' '] 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 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',''] <----这就是我不想要的东西

However…. 然而…。

If in a separate simple python file (called DOG.py) I execute the following commands on a list: 如果在一个单独的简单python文件(称为DOG.py)中,我在列表上执行以下命令:

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 = ['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) 打印(密钥1)

['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', ' '] ['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)) 打印( ''。加入(密钥1))

ABCDEFGHIJKLMNOPQRSTUVWXYZ <---- THIS IS WHAT I WANT ABCDEFGHIJKLMNOPQRSTUVWXYZ <----这就是我想要的东西

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. 我最终希望输出看起来像“ABCDEFGHIJKLMNOPQRSTUVWXYZ”,但对于我的生活,我无法弄清楚如何读取文件的列表,然后将该列表转换为单个字符串。 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 . 您必须先将string转换为list然后才能应用join

First we use partition to get the part after the = . 首先我们使用partition来获取=之后的部分。 Then ast.literal_eval will create a list out of the string. 然后ast.literal_eval将创建一个字符串列表。

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. 附加:你可以使用with在打开文件的情况下。 If you do it, Python will close the file for you. 如果你这样做,Python会为你关闭文件。

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. 正如jamylak所指出的:如果您控制文件,请不要存储这样的数据。 As you can see it's not easy to handle. 你可以看到它并不容易处理。 You could store and read the data as JSON. 您可以将数据存储和读取为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 . 使用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. 使用str.split和split by =然后通过索引选择字符列表[-1]将给出最后一个带有字符列表的索引,然后使用ast.literal_eval获取字符,然后使用str.join加入所有人物。

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. 你的问题是,在你的第一个例子中,key1是一个字符串,而不是一个列表。 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 = "" string =“”

read = open('blah.txt', 'r') read = open('blah.txt','r')

key = read.readline() key = read.readline()

a = re.compile("[AZ]") a = re.compile(“[AZ]”)

for char in key: 对于密钥中的字符:

    if a.match(char):

            string = string + char

print string 打印字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM