I'm creating a script to receive plain text and turn it into csv, to later import into Anki. I'm experimenting with pandas.read_clipboard(), to try skip using txt files. If the first line of my data only has one column (no answer attached) and the line after it does, the function breaks.
Not working
foo
foo-bar
Working
foo-bar
foo
I expected to receive
0 1
0 foo None
1 foo bar
Instead an error is thrown
pandas.errors.ParserError: Expected 1 fields in line 2, saw 2
As I'm writing this, I realize now that the function reads it as csv and expects a fixed number of columns from the beginning.
Can I bypass this or are these the limitations of this function?
code...
r1 = pandas.read_clipboard(header=None, sep='-', engine="python", on_bad_lines='warn')
print(r1)
Since your data doesn't have a header, pandas has to guess the number of columns and does that by looking at the first line. But you can tell pandas what the columns are in advance via the names
parameter. There is no need to guess and get it wrong.
>>> r1 = pandas.read_clipboard(header=None, sep='-', engine="python", on_bad_lines='warn',
... names=[0,1])
>>> print(r1)
0 1
0 foo None
1 foo bar