-2

I have this list:

comment = ['Item Location: AUMEL222\nLintu Testi: C:\\SSD\\important\\text.txt\nLintu Connection: 123\nItem Version: ABC.123.ABC\nItem Serial: 1234ABCD\nItem Configuration ID: ABCD1234']

i need to extract certain items from here. And i have made it work, but there must be more simple way to do this. My code looks like this:

key = "Item Location"
key_found = False
for line in comment:
    if key_found:
        get_value(line)     #converts the big list to more readable state.
line2 = line
teststat = ""
FW = ""
print(line2)
for item in line2.split("\n"):
        if "Item Location" in item:
               Teststat = (item.strip())
        if "Item Version" in item:
                FW = (item.strip())
print(Teststat)
print(FW)

outputs:

Item Location : AUMEL222
Item Version : ABC.123.ABC

So grabbing few wanted values from a string. The main goal is to only print out the Value. Not the key. But it can be done with:

print(Teststat.replace("Item Location: ", ""))
  • I really don't get it. Please tell me what is the input and what do you want as output. – Rahul Jul 13 '17 at 10:35
  • input is the list "comment" and wanted output is the same output but with more simple code. –  Jul 13 '17 at 10:39

4 Answers4

2

Regular expressions are often overkill but they are a good match (lame pun, sorry) here:

import re
for line in comments:
    found = re.findall(r"(^Item Location|Item Version): (.+?)$", line, re.MULTILINE)
    if found:
        print(found)
        # if you only want the values:
        print("\n".join(v for k, v in found))
        # if you want it as a dict for future use
        found = dict(found)
        print(found)
        # etc
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
1

Try to use item.split(':')[1]instead of item.strip().

0
comment = ['Item Location: AUMEL222\nLintu Testi: C:\\SSD\\important\\text.txt\nLintu Connection: 123\nItem Version: ABC.123.ABC\nItem Serial: 1234ABCD\nItem Configuration ID: ABCD1234']
key = 'Item Location'

#split with \n
commentSplit = comment[0].split('\n')
commentSplit2 = []

#split with ':'
for i in commentSplit:
    commentSplit2.append(i.split(':'))

#value for key
for i in commentSplit2:
 if i[0] == key:
     print i[1]
Sanober Sayyed
  • 172
  • 1
  • 10
-1

You may want something like this;

comment = ['Item Location: AUMEL222\n'
           'Lintu Testi: C:\\SSD\\important\\text.txt\n'
           'Lintu Connection: 123\n'
           'Item Version: ABC.123.ABC\n'
           'Item Serial: 1234ABCD\n'
           'Item Configuration ID: ABCD1234']
comment = comment[0]

for key_value_pair in [key_value_pair.split(':') 
                       for key_value_pair in comment.split('\n')]:
    print(':\t'.join(key_value_pair))

Note that I have simply put parts of your string inside the list onto new lines for readability, this hasn't made any change to the value of comment.


Outputs:

Item Location:   AUMEL222
Lintu Testi:     C: \SSD\important\text.txt
Lintu Connection:    123
Item Version:    ABC.123.ABC
Item Serial:     1234ABCD
Item Configuration ID:   ABCD1234
Tom Wyllie
  • 2,020
  • 13
  • 16
  • with: print(key_value_pair[1]) exactly what i was looking for. thank you –  Jul 13 '17 at 10:46