You must create a list from a text file

I need to return a list of pairs (datetime.date (x, y, z), the number of times it appears) in a text file with 1000 entries. Function def eventfreq(year, month): takes as parameters the year and month of the previously mentioned datetime.date parameter. So far I have determined the year and month

 def eventfreq(year, month): F=fieldict('DOT1000.txt') for line in F: year=F[1].year month=F[1].month 

fieldict is a function that returns returns a tuple (Manufacturer, datetime.date (), year, crash, inj) for each record in the file. I only need this to extract the datetime from each record. I am very new to python, I have been working on several of these functions all night.

EDIT: Example Data:

503 958504 GENERAL ENGINES CORP. CHEVROLET LUMINA 1990 19920606 N 0 0 SERVICE BRAKES, HYDRAULIC: FOUNDATION COMPONENTS: DISC: CALIPER DRACUT MA 2G1WN14T9L9 19950110 19950110 PROBLEM CALIPERS FREQUENCY MANUFACTURER. TT EVOQ

This is 1 record 1000. I need advice on creating my list of pairs [(datetime.date (), #of occurrences in the file)]. Year and month are parameters for the function

EDIT: test cases:

 >>> evlist = eventfreq(1995,1) >>> len(evlist) 17 >>> evlist[0] (datetime.date(1995, 1, 1), 5) >>> evlist[14] (datetime.date(1995, 1, 15), 1) 

EDIT: Another sample entry:

1332 477660 HONDA (AMERICAN HONDA MOTOR CO.) ACURA INTEGRA 1994 19940601 N 0 0 VISIBILITY: WINDSHIELD TUSTIN CA JH4DC4359RS 19950112 19941112 1 ISSUES OF THE WINDOW. * AK VOQ

Bold is the date. I already have a datetime.date format (an imported datetime module), and it is in one place in each record. You need a list containing pairs (datetime.date (year, month, day), the number of times in the same year and month )

+4
source share
3 answers

Assuming you know how to extract integers from your file to a list:

 import datetime dates = [19940903, 19940907, 19940801, 19950701, 19950702] formated_dates = [datetime.datetime.strptime(str(i), '%Y%m%d') for i in dates] year_month_pairs = [(i.year, i.month) for i in formated_dates] unique = set(year_month_pairs) counts = [(i, year_month_pairs.count(i)) for i in unique] 
0
source

Assuming fielddict() returns a list of tuples, for example:

 [('m1', datetime.date(1995, 1, 1), 'y1', 'c1', 'i1'), ('m2', datetime.date(1995, 1, 15), 'y2', 'c2', 'i2'), ('m3', datetime.date(1995, 1, 1), 'y3', 'c3', 'i3')] 

Your eventfreq() function could be:

 def eventfreq(year, month): F=fieldict('DOT1000.txt') #Get a list of datetimes matching year and month lst = [i[1] for i in F if((i[1].year == year) & (i[1].month == month))] #return a list of tuples (datetime, count) return [(i, lst.count(i)) for i in set(lst)] 

Your test files should now work as you expect.

0
source

Ok, I tried to get the date from the 1st word in the string matching format %Y%m%d (see this link for details), here is what it gives:

 >>> dict_dates = {} >>> from datetime import datetime >>> for line in open(r'D:\DATA\FP12210\My Documents\Temp\Python\Dates.txt'): for word in line.split(): try: # Try to convert date = datetime.strptime(word, '%Y%m%d') # Conversion succeeded dict_dates[date] = dict_dates.get(date, 0) + 1 break except: pass >>> dict_dates {datetime.datetime(1994, 6, 1, 0, 0): 1, datetime.datetime(1992, 6, 6, 0, 0): 1} >>> 

I created a file with two lines that you provided.

-1
source

Source: https://habr.com/ru/post/1444799/


All Articles