# How do I parse the string?

**URL:** <https://discuss.python.org/t/how-do-i-parse-the-string/50625>\
**Category:** Python Help\
**Created:** [April 10, 2024, 7:05am UTC](https://discuss.python.org/t/how-do-i-parse-the-string/50625 "2024-04-10T07:05:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anna17](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@anna17](https://discuss.python.org/u/anna17)\
**Post date:** [April 10, 2024, 7:05am UTC](https://discuss.python.org/t/how-do-i-parse-the-string/50625/1 "2024-04-10T07:05:50Z")

</div>

hello

There is a string below.  
FTP://21.105.28.15/abc/test1.txt  
Cut this string  
/abc/  
I just want to get the path.

How do I parse the string?

```python
import pymssql
conn = pymssql.connect('10.21.100.21', 'sa', 'abc', 'DBA', as_dict=False) 
cur = conn.cursor()

cur.execute('select path From table')
 
fetch = cur.fetchall()
 
for i in fetch:
	print (i) <==== FTP://21.105.28.15/abc/test1.txt (Cut this string /abc/ 
I just want to get the path.)

conn.close()

```

---

<div class="post-metadata">

**Author:** ![DerSchinken](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/derschinken/32/15648_2.png) [@DerSchinken](https://discuss.python.org/u/DerSchinken)\
**Post date:** [April 10, 2024, 7:58am UTC](https://discuss.python.org/t/how-do-i-parse-the-string/50625/2 "2024-04-10T07:58:21Z")

</div>

Split the string for on every `/` and then just remove the first 3 item, which will be the host, and if you don’t need the end/the file just remove the last element. Oh and I almost forgot, you need to join it back together, like:

```python
x = "FTP://21.105.28.15/abc/test1.txt"
x = x.split("/")[3:]
x = "/".join(x)
print(x)

```

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [April 10, 2024, 8:02am UTC](https://discuss.python.org/t/how-do-i-parse-the-string/50625/3 "2024-04-10T08:02:11Z")

</div>

I would use [urllib.parse — Parse URLs into components — Python 3.8.18 documentation](https://docs.python.org/3.8/library/urllib.parse.html#urllib.parse.urlparse)
