Remove quotes from text file – replace in Python3

Home Forums Quantitative Methods and Programming Remove quotes from text file – replace in Python3

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #8858

    Sometimes if you create a dataframe with rbind() in R and then save it by write.table() to a text file, all entries (numeric too) will be put in double quotes.
    To remove them one can use the following Python3 script, which is especially useful if you have a lot of files.

    
    import os
    filePath = "/path/to/dir"
    arr = os.listdir(filePath)
    for fileName in arr:
        print(fileName)
        if ".txt" in fileName:
            fl = open(filePath+fileName, 'r')
            dat = fl.read()
            fl.close()
            dat2 = dat.replace("\"", "")
            fl2 = open(filePath+fileName, 'w')
            fl2.write(dat2)
            fl2.close()
    
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.