xxxxxxxxxx
import csv
header = ['name', 'area', 'country_code2', 'country_code3']
data = ['Afghanistan', 652090, 'AF', 'AFG']
with open('countries.csv', 'w', encoding='UTF8') as f:
writer = csv.writer(f)
# write the header
writer.writerow(header)
# write the data
writer.writerow(data)
xxxxxxxxxx
import csv
with open('names.csv', 'w') as csvfile:
fieldnames = ['first_name', 'last_name']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
xxxxxxxxxx
import csv
header = ['name', 'area', 'country_code2', 'country_code3']
data = ['Afghanistan', 652090, 'AF', 'AFG']
with open('countries.csv', 'w', encoding='UTF8') as f:
writer = csv.writer(f)
# write the header
writer.writerow(header)
# write the data
writer.writerow(data)
xxxxxxxxxx
import csv
# csv header
fieldnames = ['name', 'area', 'country_code2', 'country_code3']
# csv data
rows = [
{'name': 'Albania',
'area': 28748,
'country_code2': 'AL',
'country_code3': 'ALB'},
{'name': 'Algeria',
'area': 2381741,
'country_code2': 'DZ',
'country_code3': 'DZA'},
{'name': 'American Samoa',
'area': 199,
'country_code2': 'AS',
'country_code3': 'ASM'}
]
with open('countries.csv', 'w', encoding='UTF8', newline='') as f:
writer = csv.DictWriter(f, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(rows)
Code language: PHP (php)
xxxxxxxxxx
import csv
# open the file in the write mode
with open('path/to/csv_file', 'w') as f:
# create the csv writer
writer = csv.writer(f)
# write a row to the csv file
writer.writerow(row)
xxxxxxxxxx
import com.opencsv.CSVWriter;
import java.io.FileWriter;
import java.net.URL;
public class WritingCSVFileExample {
public static void main(String[] args) throws Exception {
URL fileUrl = WritingCSVFileExample.class.getClassLoader().getResource("data.csv");
CSVWriter writer = new CSVWriter(new FileWriter(fileUrl.getFile()));
//Create record
String[] record = "2,Rahul,Vaidya,India,35".split(",");
//Write the record to file
writer.writeNext(record, false);
//close the writer
writer.close();
}
}
xxxxxxxxxx
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.supercsv.cellprocessor.Optional;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.ParseLong;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.constraint.StrRegEx;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanWriter;
import org.supercsv.io.ICsvBeanWriter;
import org.supercsv.prefs.CsvPreference;
public class WriteCSVFileExample
{
//Watch out for Exception in thread "main" java.lang.ExceptionInInitializerError
private static List<Customer> customers = new ArrayList<Customer>();
static
{
customers.add(new Customer(1, "Lokesh", "India", 12345L, "howtodoinjava@gmail.com"));
customers.add(new Customer(2, "Mukesh", "India", 34234L, "mukesh@gmail.com"));
customers.add(new Customer(3, "Paul", "USA", 52345345L, "paul@gmail.com"));
}
private static CellProcessor[] getProcessors()
{
final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+";
StrRegEx.registerMessage(emailRegex, "must be a valid email address");
final CellProcessor[] processors = new CellProcessor[] {
new NotNull(new ParseInt()), // CustomerId
new NotNull(), // CustomerName
new NotNull(), // Country
new Optional(new ParseLong()), // PinCode
new StrRegEx(emailRegex) // Email
};
return processors;
}
public static void main(String[] args)
{
ICsvBeanWriter beanWriter = null;
try
{
beanWriter = new CsvBeanWriter(new FileWriter("temp.csv"), CsvPreference.STANDARD_PREFERENCE);
final String[] header = new String[] { "CustomerId", "CustomerName", "Country", "PinCode" ,"Email" };
final CellProcessor[] processors = getProcessors();
// write the header
beanWriter.writeHeader(header);
// write the beans data
for (Customer c : customers) {
beanWriter.write(c, header, processors);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
beanWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}