Python Forum
Double Quotes will not go away, must be missing something
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Double Quotes will not go away, must be missing something
#1
I've tried using the below, and various 'csv import' machinations, and no matter what I do the double quotes are present when I open the file in notepad++, notepad, etc.  And when I bulk insert to sql serve there are coming over as an 'o' with an umlaut at the beginning and end of every column.  I'm oviously missing something with my lack of Python knowledge, but I'm at wits end as to why they just won't go away.  (They are hex value 94 if it makes a difference).  Originally opened as a csv file with excel, then format was changed to quote all but integers, and now I'm where I am.  If anybody can offer a take on that or a link, or anything,  I would appreciate it, thanks.

import os

mypath = r'C:\\csv\\'
myoutputpath = r'C:\\csv\\output\\'

for file in os.listdir(mypath): # This will loop through every file in the folder
    if '.csv' in file:  # Check if it's a csv file
        fpath = os.path.join(mypath, file)
        fpath_out = os.path.join(myoutputpath, file) #+ '_output' # Create an output file with a similar name to the input file
        with open(fpath) as infile:
            lines = infile.readlines()  # Read all lines
        with open(fpath_out, 'w') as outfile:
            for line in lines:  # One line at a time
                outfile.write(line.replace('"', '')) # Remove each " and write the line
Reply
#2
If there's quotes in the csv file, are you sure you want them removed? If there's commas within the fields, that changes the meaning of the csv file, as you're suddenly adding extra columns.

If you're still sure you want to do it, I'd start with throwing print functions everywhere to make sure things are happening as you expect. And I'd replace os.listdir() with glob.glob(".csv") and just remove the if '.csv' in file, but that's just personal preference.

It looks like it should be working, though. Aside from doing things a little oddly, I don't see anything that's just wrong.
Reply
#3
myoutputpath = r'C:\\csv\\output\\'
Having double \\ when using raw string,it become many.
>>> s = r'C:\\csv\\output\\'
>>> s
'C:\\\\csv\\\\output\\\\'
>>> # Just turn all \ around /
>>> s = 'C:/csv/output/'
>>> s
'C:/csv/output/'
Look at csv module
Can you give a sample of raw input and what you want as output
Reply
#4
I found the solution - the real issue is that the file had 'smart quotes' in it, not just straight quotes.  Finally narrowed it down to that, and the below code with the smart quote in the replace argument worked.  Thanks for the replies.

import os


mypath = 'C:\\csv\\'
myoutputpath = 'C:\\csv\\output\\'

for file in os.listdir(mypath): # This will loop through every file in the folder
    if '.csv' in file:  # Check if it's a csv file
        fpath = os.path.join(mypath, file)
        fpath_out = os.path.join(myoutputpath, file) #+ '_output' # Create an output file with a similar name to the input file
        with open(fpath) as infile:
            lines = infile.readlines()  # Read all lines
        with open(fpath_out, 'w') as outfile:
            for line in lines:  # One line at a time
                outfile.write(line.replace(u'\u201d', ''))# Remove each " and write the line
        
        infile.close()
        outfile.close()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 6,995 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  Two types of single quotes Led_Zeppelin 2 1,919 Mar-15-2021, 07:55 PM
Last Post: BashBedlam
  Remove double quotes from the list ? PythonDev 22 8,863 Nov-05-2020, 04:53 PM
Last Post: snippsat
  Quotes vs. no quotes around numbers Mark17 6 3,166 Aug-06-2020, 04:13 AM
Last Post: t4keheart
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 7,130 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  Replace all alpha characters within double quotes with underscores? walterbyrd 1 2,770 Nov-26-2017, 01:13 AM
Last Post: Larz60+
  replace string inside double quotes jmpatx 10 16,625 Apr-26-2017, 05:27 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020