Results 1 to 4 of 4

Thread: How to remove a last character from a csv file ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Posts
    64

    How to remove a last character from a csv file ?

    Hello,

    I was wondering if anyone could help me with the following problem:
    I am working with a csv file (it contains two columns of numbers), which as a last character has a comma and I would like to remove that comma from the file.

    Could ayone show me a code that might do it ?

    THANKS!

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How to remove a last character from a csv file ?

    Does every line end with a comma, or just the last one.

    Either way you will need to use something like ReadAllLines to bring the whole file into memory, then manipulate, then write, but it depends a bit on what actually needs to be removed.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2008
    Posts
    64

    Re: How to remove a last character from a csv file ?

    Hi SH !

    No, only the last line has a comma. Can I just read the last character and if it si a comma then remove it ?

    Thanks!

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How to remove a last character from a csv file ?

    I don't think you can really do that. One way or another, the entire file will pass into memory and back out again. ReadAllLines would certainly do, as it would create an array of text lines, and you would only need to look at the last string in the array. In some ways, this may be the most straightforward solution, but I'd certainly look into a different option, as well. Unfortunately, I have never tried this:

    A memory stream would allow you to see the bytes, and you could then look at the last few. I would think that a streamreader might give you the same means. On the other hand, that last line might have some hidden characters (a carriage return and line feed pair) after the comma, so this may prove to be more trouble than its worth.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width