browse by category or date

Yesterday I wrote about regex that match string inside quotes and comma separated. Today, I was given task to process CSV file and upload it into DB. The CSV file is structured like this:

Column1,Column2,Column3
"Value 1",1, "This value contains
Newline"
"Value 2",2,"One line, and has comma"
"Value 3",3,"A short, but contains comma 
and new  line &ámp; HTML encoded "

I did wrote something about parsing CSV in C# and in JavaScript before. But both failed to handle above example.

Since I am not required to convert the string value to its actual data type, I can use a simple iteration to parse the CSV.

public List<List<string>> ReadCSVFileToList(StreamReader sr, char SeparatorChar)
{
    var res = new List<List<string>>();

    var curList = new List<string>();
    var curString = "";
    var isInQuote = false;
    while (!sr.EndOfStream)
    {
        var chr = (char)sr.Read();
        switch (chr)
        {
            case '"':
                isInQuote = !isInQuote;
                break;
            case '\n':
                if (isInQuote) curString += '\n';
                else
                {
                    curList.Add(curString);
                    curString = "";
                    res.Add(curList);
                    curList = new List<string>();
                }
                break;
            default:
                if (isInQuote)
                {
                    curString += chr;
                }
                else
                {
                    if (chr == SeparatorChar)
                    {
                        curList.Add(curString);
                        curString = "";
                    }
                    else
                        curString += chr;
                }
                break;
        }
    }
    return res;
}

I hope it helps!

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

New update on travel advisory:

Sent by Gov.sg – 13 Mar]

COVID-19: Additional measures to prevent importation

➡ New travel advisory and border restrictions
– Defer non-essential travel to the whole of Italy, France, Spain and Germany
– New visitors with travel history to these countries within the last 14 days will be not be allowed into/transit through Singapore
– A 14-day Stay-Home Notice for residents and long-term pass holders with recent travel to the above countries in the last 14 days
– To exercise caution when travelling to countries with exported cases
– Full travel advisory and list of countries with exported cases: go.gov.sg/going-overseas
– Full list of restrictions: go.gov.sg/entering-singapore

➡ 14-Day Stay-Home Notice for all incoming travellers with symptoms

➡ Port calls to cease for all cruise vessels

More: go.gov.sg/importation-measures

Additional guidelines on Social-distancing

[Sent by Gov.sg – 13 Mar]

COVID-19: Additional social distancing measures

➡ Advisory for ticketed events
– Events with 250 or more participants to be deferred/cancelled
– For committed events, to have satisfactory precautionary measures in place before proceeding

➡ All other gatherings
– Reduce to below 250 participants where possible
– Put in place precautionary measures

➡ Advisories at workplaces and public venues
– Employers advised to put in place social distancing measures like tele-commuting, video-conferencing, and spacing work stations apart
– Limiting and spacing visitors apart at dining and entertainment venues, tourist attractions and indoor sports centres

More:
go.gov.sg/socialdistancing-13mar

Update on number of confirmed and discharged cases:

[Sent by Gov.sg]

COVID-19: 13 Mar Update

As of 12pm:
New cases: 13
Total cases in Singapore: 200
Discharged today: 1
Total discharged: 97
Total remaining in Hospital: 103

Of the new cases, 1 is part of the SAFRA Jurong cluster, 9 are imported, 2 are linked to previous cases, and 1 is unlinked.

Most in hospital are stable or improving. 11 are in the intensive care unit.

Go.gov.sg/moh13mar

March School Holidays
– Singapore has seen imported cases from Mainland China, France, Germany, Italy, Indonesia, Japan, Malaysia, Philippines, the UK and the US
– Please check travel advisory and review your plans
Go.gov.sg/going-overseas

Stay safe everyone!

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

Update on number of confirmed cases, and temporary closure of all mosques in Singapore:

[Sent by Gov.sg]

COVID-19: 12 Mar Update

As of 12pm:
New cases: 9
Total cases in Singapore: 187
Discharged today: 0
Total discharged: 96
Total remaining in Hospital: 91

Of the new cases, 3 are part of the SAFRA Jurong cluster, 5 are imported cases, and 1 is linked to a previous case.
Most in hospital are stable or improving. 9 are in the intensive care unit.

There is no change to the DORSCON level.
go.gov.sg/moh12mar

Temporary closure of mosques, suspension of mosque activities
– 4 mosques closed after confirmed case found to have frequented these mosques
– From 13 Mar, all mosques will be closed for 5 days for disinfection
– Mosques will cancel all mosque activities and classes for next 2 weeks
– Friday prayer at all mosques will be suspended on 13 Mar
More: go.gov.sg/mosquesclosure

PM’s update on the current Singapore condition in regards to COVID-19:

[Sent by Gov.sg – 12 Mar]

COVID-19 situation in S’pore

– More imported cases expected as global numbers rise
– More travel restrictions may be needed
– If there are many cases here, may need to hospitalise only those seriously ill; those with mild symptoms to see GP, rest at home

SG is NOT going to DORSCON Red
— We have the situation under control

Economic impact
– Measures to help affected businesses, workers
– Govt working on 2nd package of measures

Staying #SGUnited
👩‍⚕ Frontline staff working hard
💪 Singaporeans’ support crucial to overcome this

Everyone can do their part
– Practise good personal hygiene, adopt new social norms 🧼🙌
– Maintain social distancing 👋

Watch: go.gov.sg/pmo12mar

Stay safe and do social distancing, everyone!

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant: