Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
def format(table_content):
lines = table_content.split("\n")
len_info = {}
for line in lines:
cells = line.split("|")
for no, cell in enumerate(cells):
cell_len = len(cell)
if no not in len_info:
len_info[no] = cell_len
elif len_info[no]<cell_len:
len_info[no] = cell_len
new_lines = []
for line in lines:
cells = line.split("|")
for no, cell in enumerate(cells):
cells[no] = cell.ljust(len_info[no])
new_lines.append("|".join(cells))
result = "\n".join(new_lines)
return result
txt = """|= Headline|= a other\\headline|= the **big end**|
| a cell| a **big** cell| **//bold italics//**|
| next\\line| No == headline == or?| |
| | | open end|"""
print txt
print "-"*79
print format(txt)
|= Headline|= a other\headline|= the **big end**|
| a cell| a **big** cell| **//bold italics//**|
| next\line| No == headline == or?| |
| | | open end|
-------------------------------------------------------------------------------
|= Headline|= a other\headline |= the **big end** |
| a cell | a **big** cell | **//bold italics//**|
| next\line| No == headline == or?| |
| | | open end |
def format(table_content):
cells = [line.split("|") for line in table_content.splitlines()]
widths = [max(map(len, col)) for col in zip(*cells)]
return "\n".join("|".join(cell.ljust(width) for cell, width in zip(row, widths)) for row in cells)
@derdon: Keine so gute Idee, denn das ist unter Windows '\r\n' und wenn man *dass* dann unter Windows in eine Textdatei schreibt, erhält man '\r\r\n' weil das '\n' *nochmal* durch `os.linesep` ersetzt wird.
BlackJack hat geschrieben:@derdon: Keine so gute Idee, denn das ist unter Windows '\r\n' und wenn man *dass* dann unter Windows in eine Textdatei schreibt, erhält man '\r\r\n' weil das '\n' *nochmal* durch `os.linesep` ersetzt wird.
jens hat geschrieben:Ja, ich wollte es nicht in jedem Fall kürzer, sondern ehr besser/einfacher ;) Oneliner bauen mag ich nicht :)
Mein Code ist besser/einfacher ;)
Die dritte Zeile ist zugegeben grenzwertig, doch auf meine Breitenberechnung lasse ich nichts kommen. Du brauchst dafür 9 hässliche Zeilen. Außerdem splittest du 2x das `|`. Ich mach's nur einmal. Du könntest die letzte Zeile ja einmal ausspalten.
BlackJack hat geschrieben:@derdon: Keine so gute Idee, denn das ist unter Windows '\r\n' und wenn man *dass* dann unter Windows in eine Textdatei schreibt, erhält man '\r\r\n' weil das '\n' *nochmal* durch `os.linesep` ersetzt wird.
Dafür gibt es dann 'wb'
Versteh ich das richtig? Wenn ich unter Windows mit Python "\n" in eine Textdatei schreibe, dann konvertiert Python das automatisch nach "\r\n"?
helduel hat geschrieben:Versteh ich das richtig? Wenn ich unter Windows mit Python "\n" in eine Textdatei schreibe, dann konvertiert Python das automatisch nach "\r\n"?
Ja. Unter Python ist "\n" OS-unabhängig ein Newline, daher wird zum Speichern je nach OS ein anderes Line-Ending-Symbol verwendet.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
helduel hat geschrieben:Versteh ich das richtig? Wenn ich unter Windows mit Python "\n" in eine Textdatei schreibe, dann konvertiert Python das automatisch nach "\r\n"?
Ja. Unter Python ist "\n" OS-unabhängig ein Newline, daher wird zum Speichern je nach OS ein anderes Line-Ending-Symbol verwendet.
Ist das nicht imho sogar eine Sache der Windows-C-Standardbibliothek?
def format_table(table_content):
# Split and preformat every table cell
cells = []
for line in table_content.splitlines():
line_cells = []
for cell in line.split("|"):
cell = cell.strip()
if cell != "":
if cell.startswith("="):
cell += " " # Headline
else:
cell = " %s " % cell # normal cell
line_cells.append(cell)
cells.append(line_cells)
# Build a list of max len for every column
widths = [max(map(len, col)) for col in zip(*cells)]
# Join every line with ljust
lines = []
for row in cells:
cells = [cell.ljust(width) for cell, width in zip(row, widths)]
lines.append("|".join(cells))
return "\n".join(lines)
table_content = """|= Headline|= a other\\headline|= the **big end**|
| a cell|a **big** cell| **//bold italics//**|
| next\\line| No == headline == or?| |
| | | open end|"""
print format_table(table_content)
|= Headline |= a other\headline |= the **big end** |
| a cell | a **big** cell | **//bold italics//** |
| next\line | No == headline == or? | |
| | | open end |
Das Aufsplitten ist nun ein wenig komplizierter. Denn es soll besser mit zusätzlichen Leerzeichen umgegangen werden und ein Leerzeichen von und hinten ist auch nicht schlecht.