如何使用Python将文本文件转换为数组? [英] How to convert a text file to array using Python?
本文介绍了如何使用Python将文本文件转换为数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的文本文件2J4G4Bht6Gl7G6fg6T,我要将该文件转换为这样的数组; [2J,4G,4B,ht,6G,......]。任何人都可以帮忙。谢谢。
我尝试过:
我是python的新手,我仍然不知道
解决方案
参见 Python教程 - Python 3.7.2文档 [ ^ ]。这包含开始编写Python程序所需的所有信息。
假设文本文件in.txt
包含
2J4G4Bht6Gl7G6fg6T
然后是以下程序
from textwrap import wrap
infile = open( in.txt, r )
l = wrap(infile.read(), 2 )
print ( l)
输出[更新]现在,感谢 Richard [/ update]
['2J','4G','4B','ht','6G','l7','G6','fg','6T']
I have a text file like this 2J4G4Bht6Gl7G6fg6T, I am going to convert that file to an array like this ; ["2J", "4G", "4B", "ht", "6G", ...] . Anyone can help please.Thank you
What I have tried:
I am a newbie with python, I still dont have any idea
解决方案
See The Python Tutorial — Python 3.7.2 documentation[^]. This contains all the information you need to start writing Python programs.
Assuming the text filein.txt
contains
2J4G4Bht6Gl7G6fg6T
then the following program
from textwrap import wrap infile = open("in.txt", "r") l = wrap(infile.read(),2) print(l)
outputs [update]Now, it does, thanks to Richard[/update]
['2J', '4G', '4B', 'ht', '6G', 'l7', 'G6', 'fg', '6T']
这篇关于如何使用Python将文本文件转换为数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文