This article will be through a number of aspects to solve
1, the main function of the program
2, the implementation process
3, the definition of the class
4. Dynamically update each object with the generator generator and return the object
5, use the strip to remove unnecessary characters
6, rematch matching string
7, using Timestrptime to extract the string into a Time object
8. Complete code
The main function of the program
Now you have a table-like document that stores user information: The first row is a property, each property is separated by commas (,), and each row starts with a second row that is the value of each property, and each row represents one user. How do I read this document and output one user object per line?
There are 4 other small requirements:
Each document is large, and memory crashes if all the rows generated by the row are returned as a list. An object that can only be stored in one row at a time in a program.
Each string separated by commas may have double quotes (") or single quotes ('), such as" John, "to remove the quotation marks, or, if it is a number, +000000001.24, to remove the preceding + and 0, and to extract 1.24
There is time in the document, which may be 2013-10-29, or 2013/10/29 2:23:56, to turn such a string into a time type
Such a document has many, each of the attributes are different, such as this is the user's information, that is the call history. So the specific attributes in the class are generated dynamically based on the first row of the document
Implementation process
1. Definition of class
Because properties are dynamically added, property-value pairs are also dynamically added, and the class contains updateAttributes() and updatePairs() two member functions, in addition to the list attributes store properties, the dictionary attrilist stores mappings. Where init() the function is a constructor. The __attributes preceding underline indicates a private variable and cannot be called directly outside. It can be instantiated a=UserInfo() with no parameters at all.
Class UserInfo (object):
' class to restore UserInformation '
def __init__ (self):
self.attrilist={}
self.__attributes=[]
def updateattributes (self,attributes):
self.__attributes=attributes
def updatepairs (self,values): For
I in range (Len (values)):
self.attrilist[self.__attributes[i]]=values[i ]
2. Dynamically update each object with the generator (generator) and return the object
The generator is equivalent to a function that can be run more than once in a single initialization, returning one result per loop. However return , the function returns the result, and the generator yield returns the result. Each run is yield returned, and the next run yield starts from behind. For example, we implement the Fibonacci sequence, respectively, with functions and generators:
def fib (max):
N, a, b = 0, 0, 1 while
n < max:
print (b)
A, B = B, a + b
n = n + 1
"Do" Ne
We calculate the first 6 numbers of the series:
>>> fib (6)
1
1
2
3 5 8
' Done
'
If you use a generator, just print change yield it. As follows:
def fib (max):
N, a, b = 0, 0, 1 while
n < max:
yield b
A, B = B, a + b
n = n + 1
How to use:
>>> f = fib (6)
>>> F
<generator object fib at 0x104feaaa0>
>>> to I in F:
... Print (i) ...
1
1
2
3
5
8
As you can see, the builder fib itself is an object, and each execution to yield interrupts the return of a result, and continues execution the next yield line of code from. The builder can also use generator.next() execution.
In my program, the generator part of the code is as follows:
def objectgenerator (maxlinenum): Filename= '/home/thinkit/documents/usr_info/user.csv ' attributes=[] linenum=1 a=userinfo () file=open (filename) while LineNum < maxlinenum:values=[] Line=str.deco De (File.readline (), ' gb2312 ') #linecache. getline (filename, linenum, ' gb2312 ') if line== ': print ' reading fail!
Please check filename! ' Break Str_list=line.split (', ') for the item in Str_list:item=item.strip () Item=item.strip (' \ ") Item=item.strip ( ' \ ') item=item.strip (' +0* ') item=catchtime (item) if Linenum==1:attributes.append (item) else:values. Append (item) if Linenum==1:a.updateattributes (attributes) Else:a.updatepairs (values) yield a.attrilist #cha Nge to ' a ' to-use LineNum = LineNum +1
Where the a=UserInfo() class is UserInfo instantiated. Because the document is GB2312 encoded, the corresponding decoding method is used. Because the first row is a property, there is a function that stores the list of attributes, that is, the UserInfo updateAttributes(); subsequent rows are stored in a dictionary by reading the property-value pairs. p.s.pythonthe dictionary in is equivalent to mapping (map).
3. Remove unnecessary characters using the strip
From the code above, you can see str.strip(somechar) the characters that are used to remove Str before and after somechar . somecharcan be a symbol, or it can be a regular expression, as above:
Item=item.strip () #除去字符串前后的所有转义字符, such as \t,\n
item=item.strip (' \ ") #除去前后的"
item=item.strip (' \ ")
Item=item.strip (' +0* ') #除去前后的 +00...00,* means that the number of 0 can be as many as possible, or not
4.re.match Matching string
function Syntax:
Re.match (Pattern, string, flags=0)
Function parameter Description:
Parameter description
Pattern matching Regular Expressions
String to match.
Flags flag bits that control how regular expressions are matched, such as case sensitivity, multiline matching, and so on.
Returns none if the matching successful Re.match method returns a matching object. `
>>> s= ' 2015-09-18 '
>>> Matchobj=re.match (R ' \d{4}-\d{2}-\d{2} ', S, flags= 0)
>>> Print Matchobj
<_sre. Sre_match Object at 0x7f3525480f38>
1
2
3
4
5
5. Using Time.strptime to extract strings into time objects
In the time module, time.strptime(str,format) you can str convert the format into a format time object, the format common formats are:
%y Two-digit year representation (00-99)
%Y Four-digit year representation (000-9999)
%m Month (01-12)
Day of%d months (0-31)
%H 24-hour hours (0-23)
%I 12 Hours of hours (01-12)
%m minutes (00=59)
%s seconds (00-59)
In addition, you need to use the re module, the regular expression, to match the string to see whether the general time format, such as YYYY/MM/DD H:M:S, YYYY-MM-DD
In the above code, the function catchtime is to determine whether the item is a time object, and then convert it into a time object.
The code is as follows:
Import time
import re
def catchtime (item):
# Check if it's time
Matchobj=re.match (R ' \d{4}-\d{2}-\d{2} ', item, flags= 0
if matchobj!= None:
Item =time.strptime (item, '%y-%m-%d ')
#print "returned time:%s"%item Return
Item
else:
matchobj=re.match (R ' \d{4}/\d{2}/\d{2}\s\d+:\d+:\d+ ', item,flags=0)
if matchobj!= None:
Item =time.strptime (item, '%y/%m/%d%h:%m:%s ')
#print "returned time:%s"%item
return Item
Complete code:
Import collections import time Import re class UserInfo (object): ' Class to restore UserInformation ' def __init__ (self ): Self.attrilist=collections. Ordereddict () # ordered self.__attributes=[] def updateattributes (self,attributes): Self.__attributes=attributes def Updatepairs (self,values): For I in range (Len (values)): Self.attrilist[self.__attributes[i]]=values[i] def catchtime ( Item): # Check if it ' s time Matchobj=re.match (R ' \d{4}-\d{2}-\d{2} ', item, flags= 0) if matchobj!= none:item Rptime (item, '%y-%m-%d ') #print "returned time:%s"%item return item Else:matchobj=re.match (R ' \d{4}/\d{2}/\d{2}\s \d+:\d+:\d+ ', item,flags=0) if matchobj!= none:item =time.strptime (item, '%y/%m/%d%h:%m:%s ') #print "returned TI Me:%s "%item return item def objectgenerator (maxlinenum): filename= '/home/thinkit/documents/usr_info/user.csv ' att Ributes=[] Linenum=1 a=userinfo () file=open (filename) while LineNum < maxlinenum:values=[] Line=str.deCode (File.readline (), ' gb2312 ') #linecache. getline (filename, linenum, ' gb2312 ') if line== ': print ' reading fail!
Please check filename! ' Break Str_list=line.split (', ') for the item in Str_list:item=item.strip () Item=item.strip (' \ ") Item=item.strip ( ' \ ') item=item.strip (' +0* ') item=catchtime (item) if Linenum==1:attributes.append (item) else:values. Append (item) if Linenum==1:a.updateattributes (attributes) Else:a.updatepairs (values) yield a.attrilist #cha Nge to "a" to "use" LineNum = LineNum +1 if __name__ = = ' __main__ ': for N in Objectgenerator (TEN): Print n #输出字典, see if That's right
Summarize
The above is the entire content of this article, I hope to be able to learn or work to bring some help, if there is doubt you can message exchange, thank you for the cloud Habitat Community support.