python - Python - how to split a file object up into a list of individual characters?
问题描述
I'm importing data from a file object. It's pretty simple; it's a .txt file, and the data was entered like this: ABCDEFGHIJKLMNOPQRSTUVWXYZ
I am trying to get it to be a list of individual characters, e.g.
my_list = ['A', 'B', 'C', 'D', ...etc.]
but it's showing up like this:
my_list = ['ABCDEFGHIBJK... etc.]
Where am I going wrong?
def split(string):
return [char for char in string]
# This opens a file, gets a file object, and returns it to the program.
def get_file_object1():
infile = open(r'#', 'r')
file_object = infile.readlines()
testing = split(file_object) # this is a test line
print(testing) # this is a test line
print(split('This is another test.'))
infile.close()
return file_object
Note: when I pass the file object to split(file_object), I get this
['ABCDEFGHIJKLMNOPQRSTUVWXYZ']
But when I pass a string of text to split('This is another string.'), I get this:
['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', 'n', 'o', 't', 'h', 'e', 'r', ' ',
't', 'e', 's', 't', '.']
解决方案
简单,只需阅读并申请list()
;-)
with open(r'C:\Users\Liz\Desktop\PYTHON PROBLEMS\cipherUpper.txt') as f:
myList = list(f.read().strip())
获取myList
您的清单
推荐阅读
- javascript - 使用节点和猫鼬插入依赖于另一个的文档
- javascript - 使用 if-else 降序而不使用逻辑运算符 Javascript
- selenium - 在 Flutter 应用中使用 dart 抓取动态网站
- haxe - 我可以对我的代码做些什么,不要删除数组中的 0?
- angular - 如何将字符串转换为角度4中的数字
- string - 嵌套行到数组转换并访问特定值
- haskell - 在范畴论中,两个空集可以相互同构吗?
- java - 解析三重嵌套 JSON 的最简单方法是什么?
- postman - 试图在工作区中获取两个 postman.setNextRequest(未链接)或两个操作
- python - 如何从我的数据集中提取数字并制作一个数组?