Select to view content in your preferred language

split text and use parts as variables?

3514
13
Jump to solution
10-19-2018 03:47 PM
by Anonymous User
Not applicable

I want to use the numbered parts of this script in the screenshot below as variables. Any suggestions?

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

If the filename format is consistent, you could use a couple of splits:

filename = 'Bermuda Rd MP 0_26 102017_TestTable.xlsx'
f = filename.split(' MP ')
address = f[0]
mile = f[1].split(' ')[0]

print address
# Bermuda Rd

print mile
# 0_26

# and if the underscore in mile represents a decimal point
print '.'.join(mile.split('_'))
# 0.26
# or
mile = '.'.join(f[1].split(' ')[0].split('_'))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

And using listdir to get filenames in directory:

import os

path = r"C:\Users\anthonyv\Downloads\Upload"

files = os.listdir(path)
for filename in files:
    f = filename.split(' MP ')
    address = f[0]
    # mile = f[1].split(' ')[0] # keep underscore
    mile = '.'.join(f[1].split(' ')[0].split('_')) # convert underscore to period

    print address, mile‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
13 Replies
JoshuaBixby
MVP Esteemed Contributor

Can you post another shot without your boxing/highlighting?  I am trying to look closer at the structure of your list, but I think there are some important quotes being covered up.

0 Kudos
by Anonymous User
Not applicable

0 Kudos
DanPatterson_Retired
MVP Emeritus

which was I was pointing out, you made a string of your list.  Did you try removing your str(x) line to get your actual file names? and do you filenames actually contain spaces?

0 Kudos
by Anonymous User
Not applicable

Yes, my filenames contain spaces in themI tried removing the str(x) line but get the following error...

AttributeError: 'list' object has no attribute 'split'

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Can you paste the text into GeoNet?  Whenever I try to copy what I see on the screenshot, the Python interpreter keeps giving invalid syntax errors. 

0 Kudos
by Anonymous User
Not applicable

I figured out how to call one part of the file name from the first file. I'm needing to throw this in some sort of loop that will grab the same value for each file no matter how many there are at the time. For this test there are only 2 files I'm trying to grab the variable FullNm from. 

import os, sys

path = r"C:\Users\anthonyv\Downloads\Upload"
x = (os.listdir(path))
xx = str(x)
words = (xx)
words2 = words.split(" ")
RdNm,RdTyp,Mp,MpNm,Dt = words2[0],words2[1],words2[2],words2[3],words2[4]
OFullNm = "{} {}".format(RdNm,RdTyp)
FullNm = OFullNm.replace("['","")

print (FullNm)

0 Kudos
DanPatterson_Retired
MVP Emeritus

I am not sure why you want to make a string of a list. I suspect you want filenames from a folder.

#---- get the list and slice the first two as an example

pth = r"c:\Temp"

words = (os.listdir(pth))
words[:2]

['aust_temperature_demo.py', 'aust_temperature_demo.py.zip']

# ---- now see what happens when you 'str' it

words = str(os.listdir(pth))
words[:2]
"['"  # ---- ??????


words  # it is now a string

"['aust_temperature_demo.py', 'aust_temperature_demo.py.zip' ... snip ...  '__pycache__']"

# ---- get files of a certain type ... one method

py_files = [f for f in os.listdir(pth) if f[-3:] == ".py"]

py_files
['aust_temperature_demo.py', ... snip ... 'script1.py', 'script2.py']

Then split up your filenames

DanPatterson_Retired
MVP Emeritus

you don't split the list... you want to split what is in the list

a = ['a file with spaces.py', 'another one for some reason.py']

b = [i.split(' ') for i in a]  # ---- now split on the spaces
 
b

[['a', 'file', 'with', 'spaces.py'],
 ['another', 'one', 'for', 'some', 'reason.py']]
DanPatterson_Retired
MVP Emeritus
a = ['Bermuda Rd first.py', 'Bermuda Rd first.py']

b = [i.split(' ') for i in a]  # ---- now split on the spaces

c = [i[:2] for i in b]

c
[['Bermuda', 'Rd'], ['Bermuda', 'Rd']]