mercredi 17 juillet 2019

Test if Current Character is Last Character in String (Python)

Currently working on a python script to convert csv to and sql file. When the csv is generated NULLs are not inputted in to the text file. Therefore I have to parse through the data and if there are to commas next to each other (Example: ,,) then I know I need to input a null value in between the two. I'm having problems with testing to see if the last column in a record is null or not. Currently I hunt for commas, and if I find another right after, it gets set to null but at the end its just a comma and then the string ends.

I have tried looking around on here, most of what I find is similar to this link. Which to what I have found is not the solution to the issue at hand.

Take a look at a small code snippet to see how I'm currently doing it (does not work):

for text in line:
    if(text == ',' and line[indexCurrent+1]==None):
        newLine = line[indexCurrent] + '"NULL"' + line[indexCurrent+1]    

These are my current results:

From this line:

2816 ,, "Jeremie Hermiston",, "789-yb-20772",

I get this line:

2816 ,"NULL", "Jeremie Hermiston","NULL", "789-yb-20772",

I want this line:

2816 ,"NULL", "Jeremie Hermiston","NULL", "789-yb-20772","NULL"

You can also assume that the comma is the last character in the string, there is nothing (not even an empty space) after that final comma.

Aucun commentaire:

Enregistrer un commentaire