How Write into CSV File Properly

How write into CSV file properly

csv << "\n"

Stackoverflow requires 30 characters in an answer, but I don't know what more to say.

How write into CSV file properly

csv << "\n"

Stackoverflow requires 30 characters in an answer, but I don't know what more to say.

Writing data into CSV file in C#

UPDATE

Back in my naïve days, I suggested doing this manually (it was a simple solution to a simple question), however due to this becoming more and more popular, I'd recommend using the library CsvHelper that does all the safety checks, etc.

CSV is way more complicated than what the question/answer suggests.

Original Answer

As you already have a loop, consider doing it like this:

//before your loop
var csv = new StringBuilder();

//in your loop
var first = reader[0].ToString();
var second = image.ToString();
//Suggestion made by KyleMit
var newLine = string.Format("{0},{1}", first, second);
csv.AppendLine(newLine);

//after your loop
File.WriteAllText(filePath, csv.ToString());

Or something to this effect.
My reasoning is: you won't be need to write to the file for every item, you will only be opening the stream once and then writing to it.

You can replace

File.WriteAllText(filePath, csv.ToString());

with

File.AppendAllText(filePath, csv.ToString());

if you want to keep previous versions of csv in the same file

C# 6

If you are using c# 6.0 then you can do the following

var newLine = $"{first},{second}"

EDIT

Here is a link to a question that explains what Environment.NewLine does.

How to write to a CSV line by line?

General way:

##text=List of strings to be written to file
with open('csvfile.csv','wb') as file:
for line in text:
file.write(line)
file.write('\n')

OR

Using CSV writer :

import csv
with open(<path to output_csv>, "wb") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
for line in data:
writer.writerow(line)

OR

Simplest way:

f = open('csvfile.csv','w')
f.write('hi there\n') #Give your csv text here.
## Python will convert \n to os.linesep
f.close()

List not writing into a CSV file properly

For python 3:
Use writerow to write a row taken from a list(iterable)

import csv
my_list =[('so glad', 'happy'),
('it is so sad', 'sad')]
with open('mycsv.csv','w+') as fileobj:
csvwriter = csv.writer(fileobj)
for row in my_list:
csvwriter.writerow(row)

or Use writerows which takes an iterable (an iterable is anything that can be used to iterate so your my_list is an list iterable) and takes rows and saves them accordingly

import csv
my_list =[('so so glad', 'happy'),
('it is so sad', 'sad')]
with open('mycsv.csv','w+') as fileobj:
csvwriter = csv.writer(fileobj)
csvwriter.writerows(my_list)

For python 2:

with open('mycsv.csv','w+') as fileobj:

use wb instead of w+

The output in mycsv.csv is:

so so glad,happy
it is so sad,sad

Also note i used w+ as mode here which truncates that is empties the csv file if already exists or creates a new one and writes content to them

There are several modes to write use one for your need.
Take a look at the File modes documentation for python

On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files.

Writing output to csv file [in correct format]

2 options:

  1. Just do a f.write('\n') after your current f.write statement.

  2. Use csv.writer. You mention it but it isn't in your code.

    writer = csv.writer(f)
    ...
    writer.writerow([int(x) for x in row]) # Note difference in parameter format

How do I write data into CSV format as string (not file)?

You could use StringIO instead of your own Dummy_Writer:

This module implements a file-like class, StringIO, that reads and writes a string buffer (also known as memory files).

There is also cStringIO, which is a faster version of the StringIO class.

writing a csv with column names and reading a csv file which is being generated from a sparksql dataframe in Pyspark

Try

df.coalesce(1).write.format('com.databricks.spark.csv').save('path+my.csv',header = 'true')

Note that this may not be an issue on your current setup, but on extremely large datasets, you can run into memory problems on the driver. This will also take longer (in a cluster scenario) as everything has to push back to a single location.

Spark dataframe not writing Double quotes into csv file properly

It can be done by disabling both escaping and quotation

myDf.repartition(1).write
.format("com.databricks.spark.csv")
.option("escape", "")
.option("quote", "")
.option("delimiter", "|")
.save("Path to save file")

Dealing with commas in a CSV file

As others have said, you need to escape values that include quotes. Here’s a little CSV reader in C♯ that supports quoted values, including embedded quotes and carriage returns.

By the way, this is unit-tested code. I’m posting it now because this question seems to come up a lot and others may not want an entire library when simple CSV support will do.

You can use it as follows:

using System;
public class test
{
public static void Main()
{
using ( CsvReader reader = new CsvReader( "data.csv" ) )
{
foreach( string[] values in reader.RowEnumerator )
{
Console.WriteLine( "Row {0} has {1} values.", reader.RowIndex, values.Length );
}
}
Console.ReadLine();
}
}

Here are the classes. Note that you can use the Csv.Escape function to write valid CSV as well.

using System.IO;
using System.Text.RegularExpressions;

public sealed class CsvReader : System.IDisposable
{
public CsvReader( string fileName ) : this( new FileStream( fileName, FileMode.Open, FileAccess.Read ) )
{
}

public CsvReader( Stream stream )
{
__reader = new StreamReader( stream );
}

public System.Collections.IEnumerable RowEnumerator
{
get {
if ( null == __reader )
throw new System.ApplicationException( "I can't start reading without CSV input." );

__rowno = 0;
string sLine;
string sNextLine;

while ( null != ( sLine = __reader.ReadLine() ) )
{
while ( rexRunOnLine.IsMatch( sLine ) && null != ( sNextLine = __reader.ReadLine() ) )
sLine += "\n" + sNextLine;

__rowno++;
string[] values = rexCsvSplitter.Split( sLine );

for ( int i = 0; i < values.Length; i++ )
values[i] = Csv.Unescape( values[i] );

yield return values;
}

__reader.Close();
}
}

public long RowIndex { get { return __rowno; } }

public void Dispose()
{
if ( null != __reader ) __reader.Dispose();
}

//============================================

private long __rowno = 0;
private TextReader __reader;
private static Regex rexCsvSplitter = new Regex( @",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))" );
private static Regex rexRunOnLine = new Regex( @"^[^""]*(?:""[^""]*""[^""]*)*""[^""]*$" );
}

public static class Csv
{
public static string Escape( string s )
{
if ( s.Contains( QUOTE ) )
s = s.Replace( QUOTE, ESCAPED_QUOTE );

if ( s.IndexOfAny( CHARACTERS_THAT_MUST_BE_QUOTED ) > -1 )
s = QUOTE + s + QUOTE;

return s;
}

public static string Unescape( string s )
{
if ( s.StartsWith( QUOTE ) && s.EndsWith( QUOTE ) )
{
s = s.Substring( 1, s.Length - 2 );

if ( s.Contains( ESCAPED_QUOTE ) )
s = s.Replace( ESCAPED_QUOTE, QUOTE );
}

return s;
}

private const string QUOTE = "\"";
private const string ESCAPED_QUOTE = "\"\"";
private static char[] CHARACTERS_THAT_MUST_BE_QUOTED = { ',', '"', '\n' };
}


Related Topics



Leave a reply



Submit