Domanda

We coded a data based application recently. We are using Python csv module. There are whitespace problems in the data. So we are using strip() to remove the whitespaces. Now everything is fine including the calculations.

We got a requirement where we have to detect the whitespaces and report it back. Is it possible to detect the column no and the row no of the whitespace in the data.

Please help.

EDIT:

Operating System

  1. Windows 7
  2. Ubuntu
  3. " "
  4. Fedora

Please suppose this as the sample of the data. The data in the row nos. 1, 2 and 3 is fine. Row 3 doesnt have any data but it has whitespace. I want to detect this.

È stato utile?

Soluzione

if you're using strip, it should remove all white-spaces. so:

if row.strip() == ""

would be true if the row had only white spaces:

>>> " \t ".strip() == ""
True

As for fetching the data from the csv file, if you post your code it will be easier to help..

Altri suggerimenti

The question still has huge room for elaboration.

Lets assume you have a text file with the following data as you presented:

Windows 7
Ubuntu

Fedora

Interestingly from your example it seems that you are considering empty rows as whitespace. I don't see any "Columns" in the data you presented unless you consider each character a column. In that case lets see what the data actually contains in terms of characters (I'll use \s to represent a single space and \n to represent a new line:

Windows\s7\n
Ubuntu\n
\n
Fedora\n

Are you looking to just find rows which are empty? Or do you want your program to find all rows with any whitespace character such as row 1 column 8 being a space?

All in all to find empty rows in your data I'd just load up the file, read each line using the splitlines() method for strings and find the line number which is empty. Sample code is as follows

f = open("datafile.txt", "r")
data_string = f.read()
data_rows = data_string.splitlines()
row_counter = 0
while row_counter < len(data_rows):
    # If row is empty print the row number (assuming row numbers start from 0)
    if data_rows[row_counter] == "":
        print("Row number %d is empty" % row_counter)
    row_counter += 1

Alternatively if you want to point out what rows have any whitespace in them you could use the regular expression module and test the rows (lines) for existance of whitespace (represented by \s). Please read the doucmentation for the re module for a better explanation of how this works.

import re 

f = open("datafile.txt", "r")
data_string = f.read()
data_rows = data_string.splitlines()
row_counter = 0
while row_counter < len(data_rows):
    # If row has any whitespace in it, let the user know
    a = re.search("\s", data_rows[row_counter])
    if a != None:
        print("Whitespace was found at %d in row %d" % (a.start(), row_counter))
    row_counter += 1
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top