New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
csv does not round-trip for complex numbers
#98485
Comments
|
I suggest you first fix your bug. Give |
|
Those two programs are identical. You're not using |
|
@pochmann thanks!
It does not really matter. See: » ./python.exe
Python 3.12.0a0 (heads/main:ff173ed2f6, Oct 20 2022, 14:06:56) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> data = [1, 1j]
>>> with open('example.csv', 'w') as f:
... writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
... writer.writerow(data)
...
6
>>> with open('example.csv') as f:
... reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
... print(list(reader))
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
ValueError: could not convert string to float: '1j'
This was a copy-paste error, updated. |
|
Only addressing the first example, with So with complex numbers you would need to If there was no way to get the original data back, of course it would be more alarming. |


Reproduction depends on the quoting.
QUOTE_ALL
In this case, we don't have our data back, but at least it does not raise.
QUOTE_NONNUMERIC
Qouting docs:
In this case, it raises an error, while trying to convert
1jtofloat.Current docs / tests
I cannot find any mentions of
complexnumbers in tests or docs forcsv.Solutions?
QUOTE_ALLformatting andQUOTE_NONNUMERICexception. Add a test case for it and forget about itcomplexas string: in this case it will be treated as"1j". I think it is much better, because it will allow users to convert this value tocomplexmanuallycomplexnumbers. However, I don't think it is a path we should goThe text was updated successfully, but these errors were encountered: