0

I want to exports csv with python using this code

 with open(idFile+'.csv', 'wb') as csvfile:
    f = csv.writer(csvfile)

    f.writerow(["System", "Title", "Status", "result", "iStatus","Detail","Open Date","Closed Date","Status"])

    for x in range(0,len(dataDownload['item'])):
         f.writerow([dataDownload['item'][x]["system"],
                    dataDownload['item'][x]["title"],
                    dataDownload['item'][x]["stats"],
                    dataDownload['item'][x]["result"],
                    dataDownload['item'][x]["i_status"],
                    dataDownload['item'][x]["detail"],
                    dataDownload['item'][x]["open_date"],
                    dataDownload['item'][x]["closed_date"],
                    dataDownload['item'][x]["status"],
                    ])
    return f

its successfully export, but whe i want to open it with excel it makes the data look awful,it on the wrong coloumns. it starts on coloumn result

the data has newline character i already try to replace it with " " but it doesnt effect.

result on excel

swifty
  • 59
  • 8
  • Looks like `writerow()` is adding unwanted newlines between items? – smci Jul 31 '18 at 08:20
  • Are you sure you have data for the last 5 columns? Try just printing the results in the python console first to check. It looks like they're not finding any data. – D.Griffiths Jul 31 '18 at 08:23
  • Duplicate of [Python 2 CSV writer produces wrong line terminator on Windows](https://stackoverflow.com/questions/1170214/python-2-csv-writer-produces-wrong-line-terminator-on-windows#11235483). In Python 2.x on Windows, always open your file in binary mode to prevent unwanted newlines. See the specific answer I linked. – smci Jul 31 '18 at 08:23
  • Hmm, a csv file is a **text** file. Please show what it contains as text. The column `Result` starts with a double quote, which let think that the csv does uses quotes around text fields containing special characters (delimiters or new lines) but that you ignored them at import time in Excel. – Serge Ballesta Jul 31 '18 at 08:28
  • @smci OP already uses `'wb'`mode. – Serge Ballesta Jul 31 '18 at 08:29
  • Oh sorry. Then in that case are the newlines coming from unstripped newlines in the strings? Please show us the actual text file output (copy-and-paste it from Notepad/Wordpad/whatever). – smci Jul 31 '18 at 08:30
  • if i remove the result and detail it print correctly. i already to open it with r studio and it display the one that i want – swifty Jul 31 '18 at 08:54

0 Answers0