Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace null with 0 #107

Open
mohanraj-r opened this issue Dec 22, 2023 · 1 comment
Open

Replace null with 0 #107

mohanraj-r opened this issue Dec 22, 2023 · 1 comment

Comments

@mohanraj-r
Copy link

Would it be possible to replace null values with 0 ?
It would be the equivalent of the following code from pandas python lib

df = pd.DataFrame.from_records(result)
df = df.fillna(0)
@brakmic
Copy link

brakmic commented Jul 16, 2024

Hi @mohanraj-r ,

You might want to check out my fork project csv-writer-portable because the original one seems to be abandoned. It should help solve your problem.

Here's a quick code example on how you can use it:

import { createObjectCsvWriter } from 'csv-writer-portable';

const csvPath = 'test.csv';
const csvWriter = createObjectCsvWriter({
  path: csvPath,
  header: [
    { id: 'phone_number', title: 'phone_number' },
    { id: 'name', title: 'name' }
  ],
  filterFunction: (value: any) => {
    if (value === null || value === undefined || value === '') {
      return '0';
    }
    return value;
  },
  alwaysQuote: true,
  quoteEmptyFields: true
});

const data = [
  { phone_number: 9978789799, name: "John Doe" },
  { phone_number: 8898988989, name: "Bob Marlin" },
  { phone_number: undefined, name: "Max Mustermann" },
  { phone_number: null, name: "Jane Doe" },
  { phone_number: "", name: "Dummy User" }
];

async function writeCsv() {
  await csvWriter.writeRecords(data);
}

writeCsv().catch(err => console.error('Error writing CSV:', err));

You can find the npm package here and the GitHub repo here.

Best,
Harris

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants