Traceback (most recent call last): File "/home/ludovic/Bureau/csv.py", line 1, in…
- User
Traceback (most recent call last): File "/home/ludovic/Bureau/csv.py", line 1, in import pandas as pd File "/home/ludovic/.local/lib/python3.10/site-packages/pandas/init.py", line 48, in from pandas.core.api import ( File "/home/ludovic/.local/lib/python3.10/site-packages/pandas/core/api.py", line 27, in from pandas.core.arrays import Categorical File "/home/ludovic/.local/lib/python3.10/site-packages/pandas/core/arrays/init.py", line 8, in from pandas.core.arrays.categorical import Categorical File "/home/ludovic/.local/lib/python3.10/site-packages/pandas/core/arrays/categorical.py", line 3, in from csv import QUOTE_NONNUMERIC File "/home/ludovic/Bureau/csv.py", line 5, in data = pd.read_csv(filename) AttributeError: partially initialized module 'pandas' has no attribute 'read_csv' (most likely due to a circular import) import pandas as pd
Charger le fichier CSV
filename = 'souche_enterobase.csv' data = pd.read_csv(filename)
Regrouper les données par ‘project’, ‘pcr ribotype’ et ‘data source’
grouped_data = data.groupby(['Project', 'PCR Ribotype', 'Data Source','Sample','Name']).size().reset_index(name='count')
Enregistrer les résultats dans un nouveau fichier CSV
output_filename = 'resultats.csv' grouped_data.to_csv(output_filename, index=False)
- Assistant
It looks like there is a circular import issue caused by a file conflict. You have named your script
csv.py, which conflicts with the built-incsvmodule in Python. When the pandas library tries to import the built-incsvmodule, it mistakenly imports your script instead, causing the circular import error.To fix this issue, you should rename your script to something other than
csv.pyand delete any existingcsv.pycor__pycache__directory in the same folder. For example, you can name itprocess_csv.py. Then, try running the script again, and the error should be resolved. - User
ceback (most recent call last): File "/home/ludovic/Bureau/process_csv.py", line 5, in data = pd.read_csv(filename) File "/home/ludovic/.local/lib/python3.10/site-packages/pandas/util/_decorators.py", line 211, in wrapper return func(*args, **kwargs) File "/home/ludovic/.local/lib/python3.10/site-packages/pandas/util/_decorators.py", line 331, in wrapper return func(*args, **kwargs) File "/home/ludovic/.local/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 950, in read_csv return _read(filepath_or_buffer, kwds) File "/home/ludovic/.local/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 611, in _read return parser.read(nrows) File "/home/ludovic/.local/lib/python3.10/site-packages/pandas/io/parsers/readers.py", line 1778, in read ) = self._engine.read( # type: ignore[attr-defined] File "/home/ludovic/.local/lib/python3.10/site-packages/pandas/io/parsers/c_parser_wrapper.py", line 230, in read chunks = self._reader.read_low_memory(nrows) File "pandas/_libs/parsers.pyx", line 808, in pandas._libs.parsers.TextReader.read_low_memory File "pandas/_libs/parsers.pyx", line 866, in pandas._libs.parsers.TextReader._read_rows File "pandas/_libs/parsers.pyx", line 852, in pandas._libs.parsers.TextReader._tokenize_rows File "pandas/_libs/parsers.pyx", line 1973, in pandas._libs.parsers.raise_parser_error pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 825, saw 2
- Assistant
This error is occurring because there is an issue with the structure of the input CSV file (
souche_enterobase.csv). On line 825, the parser expected 1 field, but it found 2 fields. This inconsistency causes thepandasparser to raise an error.To fix this issue, you can either:
-
Manually inspect the CSV file and correct the issue on line 825. This may involve adding or removing a separator (
,or another character) as needed. -
Alternatively, you can try to automatically handle such issues using the
error_bad_lines=Falseparameter when callingpd.read_csv:
pythondata = pd.read_csv(filename, error_bad_lines=False)This will skip lines with an incorrect number of fields, but please note that this may result in losing some data.
Once you've made one of these changes, try running the script again.
-