本文整理汇总了Python中outputty.Table.read方法的典型用法代码示例。如果您正苦于以下问题:Python Table.read方法的具体用法?Python Table.read怎么用?Python Table.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类outputty.Table
的用法示例。
在下文中一共展示了Table.read方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_read_csv_should_automatically_convert_data_types
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
def test_read_csv_should_automatically_convert_data_types(self):
data = dedent('''
"spam","eggs","ham"
"42","3","2011-01-02"
"","3.14","2012-01-11"
"21","","2010-01-03"
"2","2.71",""
''')
temp_fp = tempfile.NamedTemporaryFile(delete=False)
temp_fp.write(data)
temp_fp.close()
my_table = Table()
my_table.read('csv', temp_fp.name)
os.remove(temp_fp.name)
self.assertEquals(type(my_table[0][0]), int)
self.assertEquals(type(my_table[1][0]), type(None))
self.assertEquals(type(my_table[2][0]), int)
self.assertEquals(type(my_table[3][0]), int)
self.assertEquals(type(my_table[0][1]), float)
self.assertEquals(type(my_table[1][1]), float)
self.assertEquals(type(my_table[2][1]), type(None))
self.assertEquals(type(my_table[3][1]), float)
self.assertEquals(type(my_table[0][2]), datetime.date)
self.assertEquals(type(my_table[1][2]), datetime.date)
self.assertEquals(type(my_table[2][2]), datetime.date)
self.assertEquals(type(my_table[3][2]), type(None))
示例2: test_should_import_nothing_from_empty_csv_without_exceptions
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
def test_should_import_nothing_from_empty_csv_without_exceptions(self):
temp_fp = tempfile.NamedTemporaryFile(delete=False)
temp_fp.close()
my_table = Table()
my_table.read('csv', temp_fp.name)
os.remove(temp_fp.name)
self.assertEquals(str(my_table), '')
示例3: test_should_import_data_from_csv_with_only_one_line
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
def test_should_import_data_from_csv_with_only_one_line(self):
temp_fp = tempfile.NamedTemporaryFile(delete=False)
temp_fp.write(dedent('''\
"ham","spam","eggs"
'''))
temp_fp.close()
my_table = Table()
my_table.read('csv', temp_fp.name)
os.remove(temp_fp.name)
self.assertEquals(str(my_table), dedent('''
+-----+------+------+
| ham | spam | eggs |
+-----+------+------+
''').strip())
示例4: test_write_csv_without_filename_should_return_csv_data
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
def test_write_csv_without_filename_should_return_csv_data(self):
data = dedent('''
"spam","eggs","ham"
"42","3.0","2011-01-02"
"1","3.14","2012-01-11"
"21","6.28","2010-01-03"
"2","2.71","2"
''').strip() + '\n'
temp_fp = tempfile.NamedTemporaryFile(delete=False)
temp_fp.write(data)
temp_fp.close()
my_table = Table()
my_table.read('csv', temp_fp.name)
contents = my_table.write('csv')
os.remove(temp_fp.name)
self.assertEquals(contents, data)
示例5: test_input_and_output_encoding_should_affect_read_csv
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
def test_input_and_output_encoding_should_affect_read_csv(self):
data = '"Álvaro"\n"Píton"'
temp_fp = tempfile.NamedTemporaryFile(delete=False)
temp_fp.write(data.decode('utf8').encode('iso-8859-1'))
temp_fp.close()
my_table = Table(input_encoding='iso-8859-1', output_encoding='utf16')
my_table.read('csv', temp_fp.name)
os.remove(temp_fp.name)
output = dedent('''
+--------+
| Álvaro |
+--------+
| Píton |
+--------+
''').strip().decode('utf8').encode('utf16')
self.assertEqual(str(my_table), output)
示例6: test_should_be_able_to_change_delimiters_on_read
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
def test_should_be_able_to_change_delimiters_on_read(self):
data = dedent('''
'spam';'eggs';'ham'\r
'42';'3.0';'2011-01-02'\r
'1';'3.14';'2012-01-11'\r
'21';'6.28';'2010-01-03'\r
''').strip() + '\r\n'
temp_fp = tempfile.NamedTemporaryFile(delete=False)
temp_fp.write(data)
temp_fp.close()
my_table = Table()
my_table.read('csv', temp_fp.name, delimiter=';', quote_char="'",
line_terminator='\r\n')
os.remove(temp_fp.name)
self.assertEquals(my_table[0], [42, 3.0, datetime.date(2011, 1, 2)])
self.assertEquals(my_table[1], [1, 3.14, datetime.date(2012, 1, 11)])
self.assertEquals(my_table[2], [21, 6.28, datetime.date(2010, 1, 3)])
示例7: test_read_csv_shouldnt_convert_types_when_convert_types_is_False
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
def test_read_csv_shouldnt_convert_types_when_convert_types_is_False(self):
data = dedent('''
"spam","eggs","ham"
"42","3","2011-01-02"
"","3.14","2012-01-11"
"21","","2010-01-03"
"2","2.71",""
''')
temp_fp = tempfile.NamedTemporaryFile(delete=False)
temp_fp.write(data)
temp_fp.close()
my_table = Table()
my_table.read('csv', temp_fp.name, convert_types=False)
os.remove(temp_fp.name)
for row in my_table:
for value in row:
self.assertEquals(type(value), types.UnicodeType)
示例8: test_input_encoding_should_affect_read_csv_when_using_filepointer
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
def test_input_encoding_should_affect_read_csv_when_using_filepointer(self):
data = '"Álvaro"\n"Píton"'
temp_fp = tempfile.NamedTemporaryFile()
temp_fp.write(data.decode('utf8').encode('utf16'))
temp_fp.seek(0)
my_table = Table(input_encoding='utf16', output_encoding='iso-8859-1')
my_table.read('csv', temp_fp)
output = dedent('''
+--------+
| Álvaro |
+--------+
| Píton |
+--------+
''').strip().decode('utf8').encode('iso-8859-1')
table_output = str(my_table)
temp_fp.close()
self.assertEqual(table_output, output)
示例9: test_read_csv_should_accept_filepointer
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
def test_read_csv_should_accept_filepointer(self):
data = '"Álvaro"\n"Píton"'
temp_fp = tempfile.NamedTemporaryFile()
temp_fp.write(data)
temp_fp.seek(0)
my_table = Table()
my_table.read('csv', temp_fp)
output = dedent('''
+--------+
| Álvaro |
+--------+
| Píton |
+--------+
''').strip()
table_output = str(my_table)
temp_fp.close()
self.assertEqual(table_output, output)
示例10: test_Table_should_accept_file_object_in_read_csv
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
def test_Table_should_accept_file_object_in_read_csv(self):
csv_fake = StringIO()
csv_fake.write(dedent('''\
"ham","spam","eggs"
"ham spam ham","spam eggs spam","eggs ham eggs"
"ham spam","eggs spam","eggs eggs"
'''))
csv_fake.seek(0)
my_table = Table()
my_table.read('csv', csv_fake)
self.assertEquals(str(my_table), dedent('''
+--------------+----------------+---------------+
| ham | spam | eggs |
+--------------+----------------+---------------+
| ham spam ham | spam eggs spam | eggs ham eggs |
| ham spam | eggs spam | eggs eggs |
+--------------+----------------+---------------+
''').strip())
示例11: test_read_csv_and_write_csv
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
def test_read_csv_and_write_csv(self):
data = dedent('''
"spam","eggs","ham"
"42","3.0","2011-01-02"
"1","3.14","2012-01-11"
"21","6.28","2010-01-03"
"2","2.71","2"
''').strip() + '\n'
temp_fp = tempfile.NamedTemporaryFile(delete=False)
temp_fp.write(data)
temp_fp.close()
temp_fp_2 = tempfile.NamedTemporaryFile(delete=False)
temp_fp_2.close()
my_table = Table()
my_table.read('csv', temp_fp.name)
my_table.write('csv', temp_fp_2.name)
temp_fp_2 = open(temp_fp_2.name)
contents = temp_fp_2.read()
temp_fp_2.close()
os.remove(temp_fp.name)
os.remove(temp_fp_2.name)
self.assertEquals(contents, data)
示例12: Plotter
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
class Plotter(object):
'Stores information about a plot and plot it'
def __init__(self, data=None, rows=1, cols=1, width=1024, height=768):
self.rows = rows
self.cols = cols
self._subplot_number = 0
self.fig = figure(figsize=(width / 80, height / 80), dpi=80)
self._load_data(data)
def _load_data(self, data):
self.data = Table()
self.data.read('csv', data)
def _get_new_subplot(self, projection=None):
self._subplot_number += 1
if self._subplot_number > self.rows * self.cols:
raise OverflowError('This figure can handle only %d subplots' % \
self.rows * self.cols)
if projection is not None:
return self.fig.add_subplot(self.rows, self.cols,
self._subplot_number,
projection=projection)
else:
return self.fig.add_subplot(self.rows, self.cols,
self._subplot_number)
def save(self, filename):
#self.fig.savefig(filename, bbox_inches='tight', pad_inches=0.1)
self.fig.savefig(filename)
def linear(self, title='', grid=True, style='o-', x_labels=None,
legends=True, ignore='', colors=None,
colormap=matplotlib.cm.PRGn):
if legends is None or legends is True:
legends = {header: header for header in self.data.headers}
subplot = self._get_new_subplot()
subplot.set_title(title)
subplot.grid(grid)
columns_to_plot = []
for header in set(self.data.headers) - set(ignore):
if header != x_labels and self.data.types[header] in (int, float):
columns_to_plot.append(header)
if colors is None:
color_range = linspace(0, 0.9, len(columns_to_plot))
colors = [colormap(i) for i in color_range]
for header in columns_to_plot:
subplot.plot(self.data[header], style, label=legends[header],
color=colors.pop(0))
if x_labels is not None:
subplot.set_xticklabels(self.data[x_labels])
subplot.legend()
def scatter(self, x_column, title='', grid=True, labels=True, legends=True,
style='o-', ignore='', colors=None,
colormap=matplotlib.cm.PRGn, order_by=None, ordering='asc',
x_label=None, y_lim=None, legend_location='upper center',
legend_box=(0.5, 2.2), y_label=''):
subplot = self._get_new_subplot()
subplot.set_title(title)
subplot.grid(grid)
if order_by is not None:
self.data.order_by(order_by, ordering)
if legends is True:
legends = {header: header for header in self.data.headers}
if self.data.types[x_column] in (datetime.date, datetime.datetime):
self.fig.autofmt_xdate()
if labels:
if x_label is None:
x_label = x_column
subplot.set_xlabel(x_label)
subplot.set_ylabel(y_label)
x_values = range(1, len(self.data[x_column]) + 1)
subplot.set_xlim(0, max(x_values) + 1)
columns_to_plot = []
for header in set(self.data.headers) - set(ignore):
if header != x_column and self.data.types[header] in (int, float):
columns_to_plot.append(header)
if colors is None:
color_range = linspace(0, 0.9, len(columns_to_plot))
colors = [colormap(i) for i in color_range]
for header in columns_to_plot:
if legends is None:
subplot.plot(x_values, self.data[header], style,
color=colors.pop(0))
else:
subplot.plot(x_values, self.data[header], style,
label=legends[header], color=colors.pop(0))
subplot.set_xticks(x_values)
subplot.set_xticklabels(self.data[x_column])
if y_lim is not None:
subplot.set_ylim(y_lim)
if legends is not None:
subplot.legend(loc=legend_location, bbox_to_anchor=legend_box)
self.fig.subplots_adjust(top=0.5, right=0.9)
def bar(self, title='', grid=True, count=None, bar_width=0.8, x_column='',
bar_start=0.5, bar_increment=1.0, legends=True,
x_rotation=0, colors=None, colormap=matplotlib.cm.PRGn,
y_label=None, y_lim=None, y_columns=None):
#.........这里部分代码省略.........
示例13: file
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
#!/usr/bin/env python
# coding: utf-8
# title = Exporting to a Text File
# input = 'nice-software.csv', code
# output = 'nice-software.txt'
#We can also import data from a CSV file and export it to a text file (using
#plugins, again). The data written to the text file will be the same we saw
#when executed ``print my_table`` in Example 1.
from outputty import Table
my_table = Table()
my_table.read('csv', 'nice-software.csv')
my_table.write('text', 'nice-software.txt')
示例14: str
# 需要导入模块: from outputty import Table [as 别名]
# 或者: from outputty.Table import read [as 别名]
# coding: utf-8
# title = Using MySQL plugin
#It's easy to import data from and export data to a MySQL table.
#``outputty`` automatically identify type of data and creates a table in MySQL
#for you with correct data types, so don't worry about converting everyting.
#Let's create a simple table, export it to MySQL and then import it again.
#Note: you need to change ``connection_string`` before run it.
from outputty import Table
from random import randint
# The connection string should be in the format:
# 'username:[email protected][:port]/database/table_name'
connection_string = 'root:[email protected]/testing/test_table_' + \
str(randint(0, 99999))
my_table = Table(headers=['ID', 'First name', 'Last name'])
my_table.append({'First name': 'Álvaro', 'Last name': 'Justen', 'ID': '123'})
my_table.append((456, 'Flávio', 'Amieiro'))
my_table.append(['789', 'Flávio', 'Coelho'])
my_table.write('mysql', connection_string)
print 'Table saved:'
print my_table
print 'The types identified are:', my_table.types
other_table = Table()
other_table.read('mysql', connection_string)
print
print 'Table retrieved:'
print other_table