Python CSV formatting issue when writing specific columns to output file then opening in Excel -


the problem

i have csv file contains large number of items.

the first column can contain either ip address or random garbage. other column care fourth one.

i have written below snippet of code in attempt check if first column ip address and, if so, write , contents of fourth column csv file side side.

with open('results.csv','r') csvresults:     filecontent = csv.reader(csvresults)     output = open('formatted_results.csv','w')     processedcontent = csv.writer(output)      row in filecontent:         first = str(row[0])         fourth = str(row[3])         if re.match('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', first) != none:             processedcontent.writerow(["{},{}".format(first,fourth)])         else:             continue     output.close() 

this works extent. however, when viewing in excel, both items placed in single cell rather 2 adjacent ones. if open in notepad can see each line wrapped in quotation marks. if these removed excel display columns properly.

example input

1.2.3.4,rubbish1,rubbish2,reallyimportantdata 

desired output

1.2.3.4    reallyimportantdata - 2 separate columns 

actual output

"1.2.3.4,reallyimportantdata" - single column 

the question

is there way fudge format part not write out quotations? alternatively, best way achieve i'm trying do?

i've tried writing out file , stripping lines but, despite not throwing errors, result same...

writerow() takes list of elements , writes each of column. since feeding list 1 element, being placed 1 column.

instead, feed writerow() list:

processedcontent.writerow([first,fourth]) 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -