Tabs \n in list for python - python

I have simple script in python, want return per line the values
Tabs = # and \n
SCRIPT
output = ['192.168.0.1 #SRVNET\n192.168.0.254 #SRVDATA']
output = output[0].split('#')
output.split('\n')
OUTPUT
AttributeError: 'list' object has no attribute 'split'

After you split the first time, output is a list which doesn't support .split.
If splitting on two different items, you can use a regular expression with re.split:
>>> import re
>>> output = ['192.168.0.1 #SRVNET\n192.168.0.254 #SRVDADOS']
>>> re.split(r'\n|\s*#\s*',output[0]) # newline or comment (removing leading/trailing ws)
['192.168.0.1', 'SRVNET', '192.168.0.254', 'SRVDADOS']
You may want to group the IP with a comment as well, for example:
>>> [re.split(r'\s*#\s*',line) for line in output[0].splitlines()]
[['192.168.0.1', 'SRVNET'], ['192.168.0.254', 'SRVDADOS']]

The output of the line :
output = output[0].split('#')
is actually a list. ".split" always returns a list. In your case the output looks like this:
['192.168.0.1 ', 'SRVNET\n192.168.0.254 ', 'SRVDATA']
And as the error rightly points out, a list cannot be "split" using the ".split" which it does not support.
So now if you wanna further split the list when "#" is encountered, then this can be solved by iterating through the list and calling the split function like this:
output=['192.168.0.1 ', 'SRVNET\n192.168.0.254 ', 'SRVDATA']
for i in output:
if "\n" in i:
print("yes")
output_1=i.split("\n")
This will give the "output_1" as:
['SRVNET', '192.168.0.254 ']

If you don't want to use re, then you need to apply split("\n") to each element of output[0].split("#"), then concatenate the results together again. One way to do that is
result = [y for x in output[0].split("#") for y in x.split("\n")]

Related

Python - Comma in string causes issue with strip

I have strings as tuples that I'm trying to remove quotation marks from. If there isn't a comma present in the string, then it works. But if there is a comma, then quotation marks still remain:
example = [('7-30-17','0x34','"Upload Complete"'),('7-31-17','0x35','"RCM","Interlock error"')]
example = [(x,y,(z.strip('"')))
for x,y,z in example]
The result is that quotation marks partially remain in the strings that had commas in them. The second tuple now reads RCM","Interlock error as opposed to RCM, Interlock error
('7-30-17','0x34','Upload Complete')
('7-31-17','0x35','RCM","Interlock error')
Any ideas what I'm doing wrong? Thanks!
You can use list comprehension to iterate the list items and similarly for the inner tuple items
>>> [tuple(s.replace('"','') for s in tup) for tup in example]
[('7-30-17', '0x34', 'Upload Complete'), ('7-31-17', '0x35', 'RCM,Interlock error')]
It seems like you're looking for the behaviour of replace(), rather than strip().
Try using replace('"', '') instead of strip('"'). strip only removes characters from the beginning and end of strings, while replace will take care of all occurrences.
Your example would be updated to look like this:
example = [('7-30-17','0x34','"Upload Complete"'),('7-31-17','0x35','"RCM","Interlock error"')]
example = [(x,y,(z.replace('"', '')))
for x,y,z in example]
example ends up with the following value:
[('7-30-17', '0x34', 'Upload Complete'), ('7-31-17', '0x35', 'RCM,Interlock error')]
The problem is because strip will remove only from ends of string.
Use a regex to replace ":
import re
example = [('7-30-17','0x34','"Upload Complete"'),('7-31-17','0x35','"RCM","Interlock error"')]
example = [(x,y,(re.sub('"','',z)))
for x,y,z in example]
print(example)
# [('7-30-17', '0x34', 'Upload Complete'), ('7-31-17', '0x35', 'RCM,Interlock error')]

Split variable into multiple variables in python

In python, I have a variable with 2 lines of stuff which I want to split into 2 different variables. I also want it to do it for any number of lines, for example I can change the number of lines and it automatically splits it for how many lines there are. How would I go about doing this?
If you're sometimes unsure about how many lines there are, you'd be forced to dynamically create variable names. Don't do this. This is almost always the wrong approach to take, and a better way exists.
Instead, use the list that .split() produces directly, and process the data inside of that.
If you need to further split each line in your string into two parts, you can use nested lists. Here is a simple example of the method I think you should use instead:
# your input string
string = "first line\nsecond line\nthird line\nfourth line"
# use a list to store your data, and proccses it instead.
data = []
for line in string.split('\n'):
# split each line into two parts.
first_part, second_part = line[:-4], line[-4:]
data.append([first_part, second_part])
# data:
#
# [['first ', 'line'],
# ['second ', 'line'],
# ['third ', 'line'],
# ['fourth ', 'line']]
print(data)
You can access each part of data using a certain index. For example, if you wanted to process the first line of string, you could use data[0] which yields ['first ', 'line'].
something like below
>>> a="""this is a
multiline
string"""
>>> for eachline in a.split('\n'): print(eachline)
this is a
multiline
string
when you split the string it gets converted into a list.
>>> split_list=a.split('\n')
>>> split_list
['this is a', 'multiline', 'string']
then you can access via
>>> var1=split_list[0]
>>> var1
'this is a'
and so on...
else you can run a for loop to get each item of the list
for eachline in a.split('\n'): print(eachline)

Split function on list of strings python

I need to create a list of lists which can split a large string by newline first and then semi colon. I have a list of strings by splitting input by newline. I need to now take those elements in that list and split them by semi colon but is not letting me split again.
AttributeError: 'list' object has no attribute 'split'
items = sys.stdin.read()
collectionList = [(items.split('\n'))]
for item in collectionList:
item.split(':')
Try changing the second line to
collectionList = items.split( '\n' )
The split method automatically returns a list, so you don't need to encolse items.split( '\n' ) in square brackets. Also, you might want to store the result of each semicolon splitting in another list or some other kind of variable, for further processing:
results = []
for item in collectionList:
results.append( item.split( ':' ) )
Change the second line for this line
collectionList = items.split('\n')

separate line in words by slash and use /W but avoid :

i am trying to parse a txt file with a lot of lines like this:
470115572 cms_trk_dcs_05:CAEN/CMS_TRACKER_SY1527_7/branchController00/easyCrate3/easyBoard16/channel003
i am making a dictionary where the key is the first number on the line, and the values are (for each key) the words separated by the slash "/", every one of this words is saved into a list, for example list1 gets all cms_trk_dcs_05:CAEN, list2 would be all CMS_TRACKER_SY1527_7, etc
but when i use pattern = re.split('\W',line) to split the line, it takes into account
the ":" character, i mean when i try to print cms_trk_dcs_05:CAEN it only returns cms_trk_dcs_05, how can i save in the list all the word cms_trk_dcs_05:CAEN, and save in my list all the words separated by slash
I am new at python, so i apologize if this is for dummys
anyway thank you in advance
Use split() to match first the space after the number, and then the '/':
>>> stringin = "470115572 cms_trk_dcs_05:CAEN/CMS_TRACKER_SY1527_7/branchController00/easyCrate3/easyBoard16/channel003"
>>> splitstring = stringin.split(' ')
>>> num = splitstring[0]
>>> stringlist = splitstring[1].split('/')
>>> num
'470115572'
>>> stringlist
['cms_trk_dcs_05:CAEN', 'CMS_TRACKER_SY1527_7', 'branchController00', 'easyCrate3', 'easyBoard16', 'channel003']
>>>
Or as a (less obvious) one-liner:
>>> [x.split('/') for x in stringin.split(' ')]
[['470115572'], ['cms_trk_dcs_05:CAEN', 'CMS_TRACKER_SY1527_7', 'branchController00', 'easyCrate3', 'easyBoard16', 'channel003']]
Note, though, that the second approach creates the first element as a list.
As in Trimax's comment: : (colon) is a nonword character, so to split line correctly you need to include it in pattern. Or use SiHa's answer.
About pattern, \W equals to [^a-zA-Z0-9_] (https://docs.python.org/2/library/re.html#regular-expression-syntax), so you can just add colon to it: [^a-zA-Z0-9_:]
As for second part, just use first element of result list as dict key and assign remained list to it in form of slice.
Something like this:
result_dict = {}
for line in file_lines:
line_splitted = re.split('[^a-zA-Z0-9_:]+', line)
result_dict[line_splitted[0]] = line_splitted[1:]
Note though, if your text contains lines with same numbers, you'll lose data, as when assigning new value (list of words in this case) to existing key, it will overwrite previous value.

Python: How do i manipulate the list to get the string starting with '+'?

I am comparing 2 txt files that are ls -R of the etc directory in a linux system. I compared the 2 files using difflib.differ and got this list as my result (i put the dots to keep the list short in here):
result = [' etc:\n', ' ArchiveSEL\n', ' HOSTNAME\n', ' RMCPUser\n', ...,
' qcleaner\n', '+ extraFile\n', ' rc.d\n', '+ extraFile2\n', ...,
' resolv.conf\n', ' wu-ftpd\n']
I want to be able to take the strings with the '+' sign out to do something else. how do i manipulate the list? in the example above, i want to be able to get this string "extraFile" and "extraFile2".
Thanks to all the people who posted solutions. It helps a lot and I am grateful :)
Here's what I did to get the string I wanted:
newresult = [file[2:-1] for file in result if file.startswith('+')]
to print out the strings:
for i in range (len(newresult)):
print newresult[i]
THANKS~!!! :)
You can use list comprehension:
newlist = [file[1:] for file in result if file.startswith('+')]
# ^-- gets rid of `+` at the beginning
See the string methods documentation.
And if you want to get rid of the newline character and whitespaces just do:
newlist = [file[1:].strip() for file in result if file.startswith('+')]
Another way would be to use filter(), but you cannot manipulate the string then (just want to mention it for completeness):
newlist = filter(lambda s: s.startswith('+'), result)
>>> [x.strip('+').strip() for x in result if x.startswith("+")]
['extraFile', 'extraFile2']
Improved version with stripping out '+' and whitespace/linebreaks
Try a list comprehension : [x for x in result if x[0]=='+']

Categories

Resources