I have a CSV file that has some data in it. I want to replace all the newlines within "" by some character. But the new lines outside of these quotes should stay. What is the best way to achieve this?
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
print(argv[0:])
inputfile = argv[0:]
file_object = open(argv[0:], "r")
print(file_object)
data = file.read(file_object)
strings = data.split('"')[1::2]
for string in strings:
string.replace("\r", "")
string.replace("\n", "")
print(string)
f = open("output.csv", "w")
for string in strings:
string = string.replace("\r", "")
string = string.replace("\n", "")
f.write(string)
f.close()
if __name__ == "__main__":
main(sys.argv[1])
This does not quite work, since the "" get lost as well as the ,'s.
Expected input:
“dssdlkfjsdfj \r\n ashdiowuqhduwqh \r\n”,
"3"
Expected output:
"dssdlkfjsdfj ashdiowuqhduwqh",
"3"