Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Strip One Set of Double Quotes from Strings in Python

#1
How to Strip One Set of Double Quotes from Strings in Python

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;857322&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2>
<p>When working with data, you may encounter a string or list of strings containing two (2) double quotes. This article shows you how to remove one set of these double quotes.</p>
<hr class="wp-block-separator has-alpha-channel-opacity wp-embed-aspect-16-9 wp-has-aspect-ratio"/>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write code to remove the extra set of double quotes<em>?</em></p>
<p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p>
<ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio">
<li><strong>Method </strong>1: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-startswith/" data-type="URL" data-id="https://blog.finxter.com/python-string-startswith/" target="_blank"><code>startswith()</code></a>, <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-endswith/" data-type="URL" data-id="https://blog.finxter.com/python-string-endswith/" target="_blank"><code>endswith()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="URL" data-id="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank"><code>slicing</code></a></li>
<li><strong>Method </strong>2: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex/" data-type="URL" data-id="https://blog.finxter.com/python-regex/" target="_blank"><code>regex</code></a> </li>
<li><strong>Method </strong>3: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-replace-2/" data-type="URL" data-id="https://blog.finxter.com/python-string-replace-2/" target="_blank"><code>replace()</code></a></li>
<li><strong>Method 4</strong>: Use <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/"><code>list()</code></a></li>
<li>Method 5: Use <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="URL" data-id="https://blog.finxter.com/pandas-quickstart/" target="_blank">Pandas</a></li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 1: Use startswith() and endswith()</h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-startswith/" target="_blank"><code>startswith()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-endswith/" target="_blank"><code>endswith()</code></a> in conjunction with <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="URL" data-id="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank"><code>slicing</code></a> to remove one set of double quotes from a string.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="3-4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">web_name = '""The Finxter Acadcemy""' if web_name.startswith('"') and web_name.endswith('"'): web_name = web_name[1:-1]
print(web_name)</pre>
<p>The first line in the above code snippet declares a string containing two (2) sets of double quotes and saves this to the variable <code>web_name</code>.</p>
<p>The following line calls the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/if-then-else-in-one-line-python/" data-type="URL" data-id="https://blog.finxter.com/if-then-else-in-one-line-python/" target="_blank">if</a></code> statement with the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-startswith/" target="_blank"><code>startswith()</code></a> and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-endswith/" target="_blank"><code>endswith()</code></a> functions. Both functions are passed the argument (<code>'"'</code>). </p>
<p>This statement checks to see if <code>web_name </code>starts with and ends with the above argument. If true, the code moves to the next line and, using <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" data-type="URL" data-id="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank"><code>slicing</code></a>, removes the specified character. </p>
<p>The results are output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>"The Finxter Acadcemy"</code></td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-strip-one-set-of-double-quotes-from-strings-in-python/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-UkcLQxzPA4%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 2: Use Regex</h2>
<p class="has-global-color-8-background-color has-background">You can use the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex/" data-type="URL" data-id="https://blog.finxter.com/python-regex/" target="_blank"><code>regex</code></a> method <code><a href="https://blog.finxter.com/python-regex-sub/" data-type="post" data-id="5861" target="_blank" rel="noreferrer noopener">re.sub()</a></code> to remove one set of double quotes from a string.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import re msg = '""Boost Your Python Skills""'
msg = re.sub(r'^"|"$', '', msg)
print(msg )</pre>
<p>The first line in the above code snippet imports the <a rel="noreferrer noopener" href="https://docs.python.org/3/library/re.html" data-type="URL" data-id="https://docs.python.org/3/library/re.html" target="_blank"><code>re</code></a> library. This allows access to and manipulation of strings to extract the desired result.</p>
<p>The following line declares a string containing two (2) sets of double quotes and saves this to the variable <code>msg</code>.</p>
<p>The next line uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex-sub/" data-type="URL" data-id="https://blog.finxter.com/python-regex-sub/" target="_blank"><code>re.sub()</code></a> to search this string for any occurrences of double quotes, removes the same and saves the result back to <code>msg</code>. This overwrites the original string. </p>
<p>The results are output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>"Boost Your Python Skills"</code></td>
</tr>
</tbody>
</table>
</figure>
<p>Another option is to use <a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex-sub/" data-type="URL" data-id="https://blog.finxter.com/python-regex-sub/" target="_blank"><code>re.sub()</code></a> and pass this function two (2) arguments: </p>
<ul>
<li>the string to replace and </li>
<li>the string to replace it with.</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">msg = '""Boost Your Python Skills""'
msg = re.sub('""', '"', msg)
print(msg)</pre>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>"Boost Your Python Skills"</code></td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-strip-one-set-of-double-quotes-from-strings-in-python/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F3MtrUf81k6c%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 3: Use replace()</h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/python-regex/" data-type="URL" data-id="https://blog.finxter.com/python-regex/" target="_blank"></a><code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-replace-2/" data-type="URL" data-id="https://blog.finxter.com/python-string-replace-2/" target="_blank"><code>replace()</code></a></code> to remove one set of double quotes from a string.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">mission = '""Boost Collective Intelligence""'
mission = mission.replace('""', '"')
print(mission) </pre>
<p>The first line in the above code snippet declares a string containing two (2) sets of double quotes and saves this to the variable <code>mission</code>.</p>
<p>The following appends the <code>replace()</code> function to <code>mission</code> and is passed two (2) arguments: </p>
<ul>
<li>the string to replace, and </li>
<li>the string to replace it with. </li>
</ul>
<p>The results save back to <code>mission</code>. The results are output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>"Boost Collective Intelligence"</code></td>
</tr>
</tbody>
</table>
</figure>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 4: Use list()</h2>
<p class="has-global-color-8-background-color has-background">This method passes a <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/">list</a> containing double quotes to the <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/">list</a> function. This option differs from the other options as it removes all double quotes.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">user_emails = [""'[email protected]', '[email protected]', '[email protected]', '[email protected]'""]
user_emails = list(user_emails)
print(user_emails)
</pre>
<p>The first line in the above code contains a <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/">list</a> of Finxter user’s emails with double quotes at the beginning and the end.</p>
<p>This saves to <code>user_emails</code>.</p>
<p>The following line uses a <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/">list</a> and passes <code>user_emails</code> to it as an argument. The results save back to <code>user_emails </code>and are output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>['[email protected]', '[email protected]', '[email protected]', '[email protected]']</code></td>
</tr>
</tbody>
</table>
</figure>
<p class="has-global-color-8-background-color has-background"><strong><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />Note</strong>: Converting a <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/">list</a> containing double quotes to a <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/">list</a> removes all double quotes.</p>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-strip-one-set-of-double-quotes-from-strings-in-python/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FqXOFngx0bQU%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Method 5: Use Pandas</h2>
<p class="has-global-color-8-background-color has-background">This method uses <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="URL" data-id="https://blog.finxter.com/pandas-quickstart/" target="_blank">Pandas</a> to remove all double quotes from a CSV file. </p>
<p>Contents of CSV file</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td><code>Store,Category,Product,Number<br />""Toronto"",""Jeans"",""10534"",""15""<br />""Montreal"",""Tops"",""5415"",""32""<br />""Ottawa"",""Coats"",""98341"",""22""</code></td>
</tr>
</tbody>
</table>
</figure>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import pandas as pd df = pd.read_csv('quotes.csv',header=None)
df.replace('"', '', inplace=True, regex=True)
print(df)</pre>
<p>The above example imports the <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="URL" data-id="https://blog.finxter.com/pandas-quickstart/" target="_blank">Pandas</a> library. This library allows access to and manipulation of a <a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-quickstart/" data-type="URL" data-id="https://blog.finxter.com/pandas-quickstart/" target="_blank">Pandas</a> DataFrame.</p>
<p>The following line reads in the CSV file, without the header row into the DataFrame, <code>df</code>.</p>
<p>If output to the terminal, the DataFrame appears as follows:</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td></td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>0</td>
<td>Shop</td>
<td>Category</td>
<td>Product</td>
<td>Number</td>
</tr>
<tr>
<td>1</td>
<td>Toronto””</td>
<td>Jeans””</td>
<td>10534″”</td>
<td>15″”</td>
</tr>
<tr>
<td>2</td>
<td>Montreal””</td>
<td>Tops””</td>
<td>5415″”</td>
<td>32″”</td>
</tr>
<tr>
<td>3</td>
<td>Ottawa””</td>
<td>Coats””</td>
<td>98341″”</td>
<td>22″”</td>
</tr>
</tbody>
</table>
</figure>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Note</strong>: By default, when the CSV file was imported, the beginning double quotes were removed, thus leaving the trailing double quotes.</p>
<p>Next, all occurrences of trailing quotes are replaced (removed) from the DataFrame. </p>
<p>The output of the modified DataFrame, <code>df</code> are output to the terminal.</p>
<figure class="wp-block-table is-style-stripes">
<table>
<tbody>
<tr>
<td></td>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>0</td>
<td>Shop</td>
<td>Category</td>
<td>Product</td>
<td>Number</td>
</tr>
<tr>
<td>1</td>
<td>Toronto</td>
<td>Jeans</td>
<td>10534</td>
<td>15</td>
</tr>
<tr>
<td>2</td>
<td>Montreal</td>
<td>Tops</td>
<td>5415</td>
<td>32</td>
</tr>
<tr>
<td>3</td>
<td>Ottawa</td>
<td>Coats</td>
<td>98341</td>
<td>22</td>
</tr>
</tbody>
</table>
</figure>
<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube"><a href="https://blog.finxter.com/how-to-strip-one-set-of-double-quotes-from-strings-in-python/"><img src="https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-JKVy_HliQE%2Fhqdefault.jpg" alt="YouTube Video"></a><figcaption></figcaption></figure>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Summary</h2>
<p>This article has provided five (5) ways to remove one set of double quotes from a string and all double quotes to select the best fit for your coding requirements.</p>
<p>Good Luck &amp; Happy Coding!</p>
<hr class="wp-block-separator has-alpha-channel-opacity"/>
<h2>Programmer Humor – Blockchain</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="280" height="394" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-31.png" alt="" class="wp-image-457795" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-31.png 280w, https://blog.finxter.com/wp-content/uplo...13x300.png 213w" sizes="(max-width: 280px) 100vw, 280px" /><figcaption><em>“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.”</em> <strong>source </strong> – <a href="https://imgs.xkcd.com/comics/blockchain.png" data-type="URL" data-id="https://imgs.xkcd.com/comics/blockchain.png" target="_blank" rel="noreferrer noopener">xkcd</a></figcaption></figure>
</div>
</div>


https://www.sickgaming.net/blog/2022/11/...in-python/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016