Skip to content

Commit f629204

Browse files
committed
работа с парсингом
1 parent 029ab09 commit f629204

File tree

5 files changed

+79
-29
lines changed

5 files changed

+79
-29
lines changed

diving_in_python/file2.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from os.path import exists
2+
from tempfile import gettempdir
3+
from os.path import join
4+
5+
6+
class File:
7+
add_helper = 0
8+
9+
def __init__(self, path_to_file):
10+
if not exists(path_to_file):
11+
open(path_to_file, "w").close()
12+
self.path_to_file = path_to_file
13+
14+
def __str__(self):
15+
return self.path_to_file
16+
17+
def __iter__(self):
18+
self.current = 0
19+
with open(self.path_to_file, "r") as f:
20+
self.lines = f.readlines()
21+
return self
22+
23+
def __next__(self):
24+
if len(self.lines) == self.current:
25+
raise StopIteration
26+
line = self.lines[self.current]
27+
self.current += 1
28+
return line
29+
30+
def __add__(self, obj):
31+
File.add_helper += 1
32+
new_obj = File(join(gettempdir(), str(self.add_helper)))
33+
new_obj.write(self.read() + obj.read())
34+
return new_obj
35+
36+
def read(self):
37+
with open(self.path_to_file, "r") as f:
38+
return f.read()
39+
40+
def write(self, data):
41+
with open(self.path_to_file, "w") as f:
42+
f.write(data)
43+
print(len(data))
44+
45+
46+
if __name__ == "__main__":
47+
pass

diving_in_python/file_class.py

+15-29
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,30 @@
1-
import os
2-
import tempfile
3-
import datetime
1+
import os.path, tempfile
42

53

64
class File:
5+
"""Интерфейс для работы с файлами"""
76

87
def __init__(self, file_path):
98
self.file_path = file_path
10-
self.cur_position = 0
119

12-
if not os.path.exists(file_path):
13-
with open(file_path, 'w'):
14-
pass
15-
16-
def write(self, content):
10+
def write(self, data):
1711
with open(self.file_path, 'w') as f:
18-
f.write(content)
12+
f.write(data)
1913

2014
def read(self):
21-
with open(self.file_path, 'r') as f:
15+
with open(self.file_path) as f:
2216
return f.read()
2317

24-
def __add__(self, other):
25-
new_path = os.path.join(tempfile.gettempdir(), datetime.datetime.today().strftime("%Y%m%d%H%M%S"))
26-
new_file = File(new_path)
27-
new_file.write(self.read() + other.read())
28-
return new_file
18+
def __add__(self, obj):
19+
file_name = '-'.join([self.file_path, obj.file_path])
20+
new_file_path = os.path.join(tempfile.gettempdir(), str(hash(file_name)))
21+
new_obj = File(new_file_path)
22+
new_obj.write(self.read() + obj.read())
2923

30-
def __iter__(self):
31-
return self
32-
33-
def __next__(self):
34-
with open(self.file_path, 'r') as f:
35-
f.seek(self.cur_position)
36-
line = f.readline()
37-
if not line:
38-
self.cur_position = 0
39-
raise StopIteration('EOF')
40-
self.cur_position = f.tell()
41-
return line
24+
return new_obj
4225

4326
def __str__(self):
44-
return self.file_path
27+
return self.file_path
28+
29+
def __iter__(self):
30+
return open(self.file_path, 'r')

heartbeat_market/usd_spread.sqlite

0 Bytes
Binary file not shown.

parsing/book_parsing/6.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from urllib.request import urlopen
2+
from io import StringIO
3+
import csv
4+
data = urlopen('http://pythonscraping.com/files/MontyPythonAlbums.csv').read().decode('ascii', 'ignore')
5+
dataFile = StringIO(data)
6+
csvReader = csv.reader(dataFile)
7+
for row in csvReader:
8+
print(row)

parsing/book_parsing/8.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from urllib.request import urlopen
2+
from io import StringIO
3+
import csv
4+
data = urlopen('http://pythonscraping.com/files/MontyPythonAlbums.csv').read().decode('ascii', 'ignore')
5+
dataFile = StringIO(data)
6+
dictReader = csv.DictReader(dataFile)
7+
print(dictReader.fieldnames)
8+
for row in dictReader:
9+
print(row)

0 commit comments

Comments
 (0)