The challenge you will have, and what I tried to pretrain with my chat share, is that tab separated value files are not strictly about inserting a single tab character instead of a comma and space character. It is that they should include the presentation where the columns are aligned if seen on a monospace display.
This is made difficult by tab tokens not representing a fixed number of spaces when rendered, and being often combined or not combined with more tabs or other characters when encoded as tokens. The AI is basically destined to fail at the task.
You might write a function “csv_to_tsv” that the AI can call and from which it must repeat verbatim (or just grab its output). It would have two distinct uses, if you want just literally “tab-separated” or if you need columns aligned when printed to a device with a particular indent position per tab character.
I just had a chatbot answer about what python facilities are available for the latter:
Python’s built-in csv
module can be used to create TSV files. Here’s how you can do it:
- To simply insert tabs instead of commas in tables, you can use the
csv.writer
object with the delimiter
parameter set to ‘\t’ (which represents a tab character). Here’s an example:
import csv
rows = [['Name', 'Age', 'Profession'], ['John', '25', 'Engineer'], ['Doe', '30', 'Doctor']]
with open('output.tsv', 'w', newline='') as f_output:
tsv_output = csv.writer(f_output, delimiter='\t')
tsv_output.writerows(rows)
- For the output format that considers the monospace width of text columns, Python does not provide a built-in way to do this. However, you can write a custom function to calculate the length of each cell data and insert the required number of tabs. Here’s a simple example:
def write_tsv_with_formatting(rows, filename):
max_lengths = [max(map(len, col)) for col in zip(*rows)]
with open(filename, 'w', newline='') as f_output:
for row in rows:
line = '\t'.join(f"{val}\t{':' * (length - len(val) // 4)}"
for val, length in zip(row, max_lengths))
f_output.write(line + '\n')
rows = [['Name', 'Age', 'Profession'], ['John', '25', 'Engineer'], ['Doe', '30', 'Doctor']]
write_tsv_with_formatting(rows, 'output.tsv')
In this example, we first calculate the maximum length of each column, then for each row, we add extra tabs based on the difference between the maximum length and the length of the current cell. The // 4
part is to account for the fact that a tab character is typically equivalent to 4 spaces.
Also, AI answers knowledge retrieved from the back of my brain upon pondering output devices and obsolete formats you could punch card and run to a chain printer:
Bot: blah blah…
Here are some examples of how to set tab stops on these printers:
- On a dot matrix or daisy wheel printer, you might send a control code like
ESC D n1 n2 n3 0
, where ESC D
is the command to set horizontal tab stops, and n1
, n2
, n3
, etc., are the column positions for the tab stops.
- On a laser or inkjet printer using PostScript, you might set tab stops by including a command like
/tabs [n1 n2 n3] def
in the PostScript code, where n1
, n2
, n3
, etc., are the positions for the tab stops.