Home > Mobile >  How to create a number of strings based of an array imported from a filetex?
How to create a number of strings based of an array imported from a filetex?

Time:02-10

I'm currently very new to python programming and encountered such problem I have a textfile with strings separated by spaces and by new lines like this:

UR 11199 TPO 0625 APF 1371 ABS 1126 ABT 0475 ASL 0518

Im importing with numpy np.str and is printing out like this

`
[['UR' '11199']
 ['TPO' '0625']
 ['APF' '1371']
 ['ABS' '1126']
 ['ABT' '0475']
 ['ASL' '0518']]`

My thing is that for each line I want to condense the letters with the numbers and make a single string so that the user might type anything beyond, complementing the string like this:

UR11199 OK

How should I do this? tried using hsplit but didnt worked out for me

##EDIT## Maybe I wasnt clear enough, English isnt my first language. I need to have different strings for each line, so that a user can input data for each line like

string a == UR11199 abcd

string b == TPO0625 EFGH

CodePudding user response:

What about that:

mys = "UR 11199 TPO 0625  APF 1371 ABS 1126 ABT 0475 ASL 0518"
for i, word in enumerate(mys.split()):
    print(word, end="" if i % 2 == 0 else "\n")

output:

UR11199
TPO0625
APF1371
ABS1126
ABT0475
ASL0518

CodePudding user response:

You can simply use a list comprehension along with .join() like so:

import numpy as np

test = np.array([['UR' '11199'],
                 ['TPO' '0625'],
                 ['APF' '1371'],
                 ['ABS' '1126'],
                 ['ABT' '0475'],
                 ['ASL' '0518']])

string = " ".join([l[0] for l in list(test)])
print(string)

Output:

UR11199 TPO0625 APF1371 ABS1126 ABT0475 ASL0518

If you want each string separate you could just use:

stringList = [l[0] for l in list(test)]
print(stringList)

Output:

['UR11199', 'TPO0625', 'APF1371', 'ABS1126', 'ABT0475', 'ASL0518']
  • Related