Click here to Skip to main content
14,428,179 members
Rate this:
Please Sign up or sign in to vote.
See more:
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
Posted
Updated 20-Feb-19 23:57pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

See The Python Tutorial — Python 3.7.2 documentation[^]. This contains all the information you need to start writing Python programs.
   
Comments
CPallini 21-Feb-19 7:34am
   
My 5 as well.
Richard MacCutchan 21-Feb-19 7:37am
   
But, as you know, (almost) any fool can use Google. :)
CPallini 21-Feb-19 8:12am
   
That's what I did. Found a page on Python file I/O, another one on string splitting and, eventually patched them together.
Richard MacCutchan 21-Feb-19 8:26am
   
But your solution really answered the question.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Assuming the text file in.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']
   
v3
Comments
Richard MacCutchan 21-Feb-19 7:23am
   
+5, but your import line is duplicated.
CPallini 21-Feb-19 7:34am
   
Fixed now, thank you very much, Richard.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100