Python Forum
Double Quotes will not go away, must be missing something - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Double Quotes will not go away, must be missing something (/thread-3188.html)



Double Quotes will not go away, must be missing something - smiths87 - May-03-2017

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



RE: Double Quotes will not go away, must be missing something - nilamo - May-03-2017

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.


RE: Double Quotes will not go away, must be missing something - snippsat - May-03-2017

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


RE: Double Quotes will not go away, must be missing something - smiths87 - May-03-2017

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()