Saturday, March 23, 2019

Spilt and Join with datautil

==========Spilt ==============

0)import date fucntion
from datetime import datetime
1) declare varaible

```
string = "2018/01/02" "  " "CCCC"
print string
#'2018/01/02  CCCC'



2) split into two element


```
string.rstrip().split('  ')
#['2018/01/02', 'CCCC']
print string
#2018/01/02  CCCC
```

3) declare varable to element 0 and 1
```
>>> string = "2018/01/02" "  " "CCCC"
>>> element0=string.split()[0]
>>> element1=string.split()[1]
print (element0)
#2018/01/02
print element1
#CCCC
```

 4) convert date format


```
date_convert =(datetime.strftime(datetime.strptime(element0, '%Y/%m/%d'), '%Y%m%d'))
print date_convert 
#20180102

```



5) add two element together


```
new = date_convert +" " +element1
print new
#'20180102 CCCC'

```

 ==========Join/spit==============


spit:
```
>>> a = "this is a string"
>>> a = a.split(" ") # a is converted to a list of strings. 
>>> print a
['this', 'is', 'a', 'string']
```

Joining a string
```
>>> a = "-".join(a)
>>> print a
this-is-a-string 

```



===========datautil with spilt and join full code ==============


 Example 1:

 hoilday.txt

2010/02/15  aaaa
2010/04/15  aaaa    
2010/03/15  aaaa

import datetime as dt
with open('holiday.txt') as f, open('new.txt', 'w') as f_out:
    for line in f:
        line = line.strip()
 line =line.replace('/', '')
 f_out.write('{}\n'.format(line))
 
 

output:
20100315  aaaa



 Example 2:

 hoilday.txt
2010/02/15
import datetime as dt
from datetime import datetime
with open('holiday.txt') as f, open('new.txt', 'w') as f_out:
    for line in f:
        line = line.strip()
 #line =line.replace('/', '')
 #f_out.write('{}\n'.format(line))
 f_out.write(datetime.strftime(datetime.strptime(line, '%Y/%m/%d'), '%Y%m%d'))
output:
20100215


 Example 3:

 hoilday.txt
2010/02/15  aaaa
2010/04/15  aaaa    
2010/03/15  aaaa
import datetime as dt
from datetime import datetime
with open('holiday.txt') as f, open('new.txt', 'w') as f_out:
    for line in f:
        line = line.strip()
 #line =line.replace('/', '')
 #f_out.write('{}\n'.format(line))
 #f_out.write(datetime.strftime(datetime.strptime(line, '%Y/%m/%d'), '%Y%m%d'))
 line.rstrip().split('  ')
 line_1 = line.split()[0]
 line_2 = line.split()[1]
 newline =(datetime.strftime(datetime.strptime(line_1, '%Y/%m/%d'), '%Y%m%d'))
 #result = line_1 +" " +line_2
 #f_out.write(result)
 f_out.write(line_1 +" " +line_2+ '\n')
output:
2010/02/15 aaaa
2010/04/15 aaaa
2010/03/15 aaaa

 Example 4: improvement from example3

 hoilday.txt

2010/02/15  aaaa
2010/04/15  aaaa    
2010/03/15  aaaa
from datetime import datetime
with open('holiday.txt') as f, open('new.txt', 'w') as f_out:
    for line in f:
        line_1, line_2 = line.rstrip().split('  ')
        new_line_1 =(datetime.strftime(datetime.strptime(line_1, '%Y/%m/%d'), '%Y%m%d'))
        f_out.write('{} {}\n'.format(new_line_1, line_2))
output:
2010/02/15 aaaa
2010/04/15 aaaa
2010/03/15 aaaa

No comments:

Post a Comment