-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathconfigure_stream.py
executable file
·45 lines (34 loc) · 1.13 KB
/
configure_stream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python3
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""
import io
import pytablewriter as ptw
def main() -> None:
writer = ptw.MarkdownTableWriter(
table_name="zone",
headers=["zone_id", "country_code", "zone_name"],
value_matrix=[
["1", "AD", "Europe/Andorra"],
["2", "AE", "Asia/Dubai"],
["3", "AF", "Asia/Kabul"],
["4", "AG", "America/Antigua"],
["5", "AI", "America/Anguilla"],
],
)
# writer instance writes a table to stdout by default
writer.write_table()
writer.write_null_line()
# change the stream to a string buffer to get the output as a string
# you can also get tabular text by using dumps method
writer.stream = io.StringIO()
writer.write_table()
print(writer.stream.getvalue())
# change the output stream to a file
with open("sample.md", "w") as f:
writer.stream = f
writer.write_table()
# or you can use dump method to file if you just output a table to a file
# writer.dump("sample.md")
if __name__ == "__main__":
main()