`csv` does not round-trip for `complex` numbers
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 45/100
Rechercherichtung
Beginne damit, die im Issue gemeldeten Python-csv-Beispiele für QUOTE_ALL und QUOTE_NONNUMERIC zu reproduzieren, und sieh dir anschließend die in der Anfrage erwähnte csv-Dokumentation und die Tests an. Ermittle das beabsichtigte Verhalten für komplexe Zahlen, implementiere die vereinbarte Richtung und ergänze passende Dokumentation oder Testabdeckung, damit Round-Trip- und Konvertierungsverhalten eindeutig sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Reproduction depends on the quoting.
QUOTE_ALL
import csv
data = [1, 1j]
with open('example.csv', 'w') as f:
writer = csv.writer(f, quoting=csv.QUOTE_ALL)
writer.writerow(data)
with open('example.csv') as f:
contents = f.read()
print(contents) # 1,1j
reader = csv.reader(contents, quoting=csv.QUOTE_ALL)
print(list(reader)) # [['1'], ['', ''], ['1'], ['j'], []]
In this case, we don't have our data back, but at least it does not raise.
QUOTE_NONNUMERIC
Qouting docs:
No automatic data type conversion is performed unless the QUOTE_NONNUMERIC format option is specified (in which case unquoted fields are transformed into floats)
import csv
data = [1, 1j]
with open('example.csv', 'w') as f:
writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC)
writer.writerow(data)
with open('example.csv') as f:
contents = f.read()
print(contents) # 1,1j
reader = csv.reader(contents, quoting=csv.QUOTE_NONNUMERIC)
print(list(reader))
# Traceback (most recent call last):
# File "/Users/sobolev/Desktop/cpython/ex.py", line 11, in <module>
# print(list(reader))
# ^^^^^^^^^^^^
# ValueError: could not convert string to float: 'j'
In this case, it raises an error, while trying to convert 1j to float.
Current docs / tests
I cannot find any mentions of complex numbers in tests or docs for csv.
Solutions?
- We can say that it works as expected: with this strange
QUOTE_ALLformatting andQUOTE_NONNUMERICexception. Add a test case for it and forget about it - We can try to quote
complexas 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 tocomplexmanually - Forbid writting
complexnumbers. However, I don't think it is a path we should go
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 36k
- Ø Merge
- 1 T. 9 Std.
- Gemergte PRs (30 T.)
- 558
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/cpython
-
docs pending
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
stdlib type-feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
stdlib type-feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
build type-bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
stdlib topic-email type-feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
Ähnliche Issues
-
area/auth bug comp/agent P3 platform/discord type/security
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
NousResearch/hermes-agent#117848 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
bancolombia/sentinel#23 ·
-
test md OffenCI
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100