Author: Not specified Language: python
Description: Not specified Timestamp: 2017-08-03 20:59:23 +0000
View raw paste Reply
  1. [evad@e-vad:~]
  2. [20:52:27] $ cat dayid.py
  3. import pandas as pd
  4. import csv
  5.  
  6. file1 = "/home/evad//dayid_file_1"
  7. file2 = "/home/evad//dayid_file_2"
  8.  
  9. # example with pd bypassing validation and allowing the selection of numbered columns.
  10. df1 =  pd.read_csv(file1, header=None, delim_whitespace=True)
  11. df2 =  pd.read_csv(file2, header=None, delim_whitespace=True)
  12. # Select column 2
  13. print "File 1, column 2"
  14. print df1[1]
  15. print "File 2, column 2"
  16. print df2[1]
  17.  
  18. # Doing a thing in csv
  19. # This reads in the file with space as a delim, and checks that column 2 isn't
  20. # empty. From your source data this looks like it should have a thing in it.
  21. # Ref: https://stackoverflow.com/questions/19252588/how-do-i-test-for-null-list-entry-in-python-list
  22.  
  23. # Open the csv
  24. with open(file1, "rb") as f:
  25.   # Set space to the delim. If you want tabs use '\t'
  26.   reader = csv.reader(f, delimiter=' ')
  27.   # Iterate over the thing
  28.   for row in reader:
  29.     try:
  30.       # Is the 3 column null/none, etc
  31.       None in row[2]
  32.     except Exception as e:
  33.       print "This field should not be empty"
  34.  
  35. [evad@e-vad:~]
  36. [20:51:46] $ cat dayid_file_1
  37. this      is                   wide
  38. that      is                   wider
  39.  
  40. [evad@e-vad:~]
  41. [20:51:53] $ cat dayid_file_2
  42. thisisreallylong              so     whocares
  43. thisisevenmorereallylonger    so    itneedstomatch
  44.  
  45. [evad@e-vad:~]
  46. [20:53:02] $ sudo python ./dayid.py
  47. File 1, column 2
  48. 0    is
  49. 1    is
  50. Name: 1, dtype: object
  51. File 2, column 2
  52. 0    so
  53. 1    so
  54. Name: 1, dtype: object
  55. This field should not be empty
  56. This field should not be empty
View raw paste Reply