I created a Python script where I want to read a file, preprocess it and write it as a CSV file.
However, when I try to create a new line on every ")"-character I get a problem that says: "new-line character seen in unquoted field - do you need to open the file in universal-newline mode?" I've seen some suggesting trying to use both 'rU' and 'rb' as mode parameters, and I tried this without luck. Any other suggestions?
This is what my script looks like:
with open('testlogs', 'r', newline='') as log_file, open('output.csv', 'w') as csv_file:
contents = log_file.read()
dataPoints = re.findall('\[(.*?)\]', contents)
parsedLog = [item.replace(' ', '').replace('(', '').replace(')', '\n') for item in dataPoints]
reader = csv.reader(parsedLog)
writer = csv.writer(csv_file)
writer.writerows(reader)
i suggest using re.sub (python) to manipulating regex
import fileinput
import re
logfile="error.log"
outfile="clean.csv"
with open("error.log") as f:
newText = f.read()
newText = re.sub(r" ", "", newText)
newText = re.sub(r"(", "", newText)
newText = re.sub(r")", "\n", newText)
with open("clean.csv", "w") as f:
f.write(newText)
if you want to add some parameter in first line .csv file, just add it at the end of source code above:
for line in fileinput.input(files=['clean.csv'], inplace=True):
if fileinput.isfirstline():
print 'parameter1,parameter2,parameter3, .... '
print line,
hope this answer help you ^_^ cheers