Michael
Michael

Reputation: 688

python string manipulation

Going to re-word the question.

Basically I'm wondering what is the easiest way to manipulate a string formatted like this:

Safety/Report/Image/489

or

Safety/Report/Image/490

And sectioning off each word seperated by a slash(/), and storing each section(token) into a store so I can call it later. (Reading in about 1200 cells from a CSV file).

Upvotes: 2

Views: 322

Answers (3)

j_syk
j_syk

Reputation: 6621

If your csv is line seperated:

#do something to load the csv 
split_lines = [x.strip() for x in csv_data.split('\n')]
for line_data in split_lines:
    split_parts = [x.strip() for x in line_data.split('/')]
    # do something with individual part data
    # such as some_variable = split_parts[1] etc
    # if using indexes, I'd be sure to catch for index errors in case you
    #   try to go to index 3 of something with only 2 parts

check out the python csv module for some importing help (I'm not too familiar).

Upvotes: 0

joaquin
joaquin

Reputation: 85605

The answer for your question:

>>> mystring = "Safety/Report/Image/489"
>>> mystore = mystring.split('/')
>>> mystore
['Safety', 'Report', 'Image', '489']
>>> mystore[2]
'Image'
>>> 

If you want to store data from more than one string, then you have several options depending on how do you want to organize it. For example:

liststring = ["Safety/Report/Image/489",
              "Safety/Report/Image/490",
              "Safety/Report/Image/491"]

dictstore = {}            
for line, string in enumerate(liststring):
    dictstore[line] = string.split('/')

print dictstore[1][3]
print dictstore[2][3]

prints:
490
491

In this case you can use in the same way a dictionary or a list (a list of lists) for storage. In case each string has a especial identifier (one better than the line number), then the dictionary is the option to choose.

Upvotes: 3

jcfollower
jcfollower

Reputation: 3158

I don't quite understand your code and don't have too much time to study it, but I thought that the following might be helpful, at least if order isn't important ...

in_strings = ['Safety/Report/Image/489',
              'Safety/Report/Image/490',
              'Other/Misc/Text/500'
             ]

out_dict = {}             

for in_str in in_strings:
    level1, level2, level3, level4 = in_str.split('/')

    out_dict.setdefault(level1, {}).setdefault(
                        level2, {}).setdefault(
                        level3, []).append(level4)                        
print out_dict

{'Other': {'Misc': {'Text': ['500']}}, 'Safety': {'Report': {'Image': ['489', '490']}}}

Upvotes: 0

Related Questions