1

I am trying to analyse a data set (from a CSV file with 100,000+ rows) which includes the date and time for each occurrence in column 1 (in the format YYYY-MM-DD HH:MM:SS) of the file.

Specifically, I am trying to identify the most popular/ busiest day of the week. Drawing on Python most common element in a list and Convert Date String to Day of Week as well as strftime & strptime documentation the code I have written is:

import csv
import collections
import datetime

popular_day = collections.Counter()

with open('data.csv') as input_file:
    for row in csv.reader(input_file, delimiter=','):
        popular_day += datetime.datetime.strptime(str([row[1][0:10]]), '%B %d, %Y').strftime('%A')

print ('Most popular day: ' + str(popular_day).most_common(1))

With Sublime Text Editor, this returns the error

File "import.py", line 9 popular_day += datetime.datetime.strptime(str([row[1][0:10]]), '%B %d, %Y').strftime('%A') ^ TabError: inconsistent use of tabs and spaces in indentation

With my beginners knowledge of Python this indentation seems logical. At any rate I have tried different indentations without success and have follow the guidelines here "inconsistent use of tabs and spaces in indentation" (useful if anyone is reading this and just started out with Python).

Do I need to break down line 9 into separate functions - where one converts into days of the week format and the other deals with frequency? I can't understand why this would impact indentation, unless it stems from some other underlying problem.

Alex
  • 87
  • 8
  • 2
    make sure there are only tabs or only spaces in your file. There usually is an option in most any editor to make whitespaces visible. Just google editor_name+show whitespaces to find how to do it for your editor. Then check if you have a mix of tabs and spaces – FlyingTeller Feb 16 '18 at 10:18
  • @khelwood I have read through https://stackoverflow.com/questions/5685406/inconsistent-use-of-tabs-and-spaces-in-indentation and implemented solution from Lennart Regebro which had no impact on the problem. I have asked here if my error might be caused by some other underlying issue, which I do not believe is addressed in the other question. – Alex Feb 16 '18 at 10:28
  • It is caused by mixing tabs with spaces in your indentation. There isn't really any other answer to give. – khelwood Feb 16 '18 at 10:30
  • @Alex Your error isn't caused by some other underlying issue. Python is pretty specific there: your error comes from mixing tabs and spaces in the indentation (ie the first few blank characters of the line) of line 9. Error is contained between the start of line 9 and `popular_day`. – jadsq Feb 16 '18 at 10:36

0 Answers0