Python-打开文件-readline-列表-转换为字符串 [英] Python - open file - readline - list - convert to string

查看:786
本文介绍了Python-打开文件-readline-列表-转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题/问题详细信息

我有一个文件(blah.txt),其内容(列表)如下:

I have a file (blah.txt) where its contents (a list) look like this:

键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',''

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',' ']

在一个简单的python文件(称为CAT.py)中,我执行以下命令:

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)

键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',''

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))

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

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

但是....

如果在单独的简单python文件(称为DOG.py)中,则在列表上执行以下命令:

If in a separate simple python file (called DOG.py) I execute the following commands on a list:

键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','']

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)

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','']

['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))

print(''.join(Key1))

ABCDEFGHIJKLMNOPQRSTUVWXYZ< ----这就是我想要的

ABCDEFGHIJKLMNOPQRSTUVWXYZ <---- THIS IS WHAT I WANT

问题/疑问

我最终希望输出看起来像"ABCDEFGHIJKLMNOPQRSTUVWXYZ",但是对于我来说,我无法弄清楚如何读取文件列表,然后将该列表转换为单个字符串.有人可以告诉我我哪里出了问题,还可以告诉我一些代码来解决我的问题吗?

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?

推荐答案

>>> 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 '

这篇关于Python-打开文件-readline-列表-转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆