Traceback (most recent call last): File "C:\Users\honey\Desktop\TXT_EXCEL\TXT_EXCEL.py", line 100, in <module> _df = deal_txt(_path) ^^^^^^^^^^^^^^^
时间: 2023-08-02 22:05:07 浏览: 197
这个错误提示不完整,缺少了具体的错误信息。不过根据代码的结构来看,这个错误可能是由于deal_txt函数内部出现了错误导致的。你可以尝试打印出具体的错误信息,例如将deal_txt函数修改如下:
```python
def deal_txt(path):
with open(path,'rb') as f:
result=chardet.detect(f.read())
try:
df = pd.read_csv(path, sep='|', header=None, encoding=result['encoding'] )
except Exception as e:
print(f"Error reading file {path}: {e}")
return None
return df
```
这样可以将读取文件时出现的错误打印出来,方便你进行调试。
相关问题
Traceback (most recent call last): File "C:\Users\honey\Desktop\TXT_EXCEL\TXT_EXCEL.py", line 105, in <module> _df.to_excel(f'./{_path.stem}.xlsx', index=False, header=False) ^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'to_excel' Error reading file 9762IT.CTSM0V30_20230201_20230228.txt:'gb2312' codec can't decode byte 0xab in position 26923: illegal multibyte sequence
这个错误提示在第105行,说明是在将DataFrame转换为Excel文件时出现了错误。根据错误提示,可能是由于DataFrame为空导致的(即deal_txt返回了None)。根据deal_txt函数的代码,当读取文件出现错误时,会返回None。因此,你需要在主函数中对_df进行判断,只有当_df不为空时才进行转换为Excel文件的操作。
此外,根据错误提示,文件编码为'gb2312',但是在使用chardet.detect函数时,可能会出现误判的情况。因此,你可以在deal_txt函数中加入一个判断,如果检测到的编码不是'gb2312',就使用'gbk'编码进行读取。
修改后的代码如下:
```python
def deal_txt(path):
with open(path,'rb') as f:
result=chardet.detect(f.read())
try:
if result['encoding'] != 'gb2312':
df = pd.read_csv(path, sep='|', header=None, encoding='gbk' )
else:
df = pd.read_csv(path, sep='|', header=None, encoding=result['encoding'] )
except Exception as e:
print(f"Error reading file {path}: {e}")
return None
return df
for _path in Path('./').glob('*.txt'):
_df = deal_txt(_path)
if _df is not None:
_df.to_excel(f'./{_path.stem}.xlsx', index=False, header=False)
deal_style(f'./{_path.stem}.xlsx')
```
这样就可以避免出现空DataFrame转换为Excel文件的情况,同时对于文件编码不确定的情况也进行了处理。
解释下F:\python_projects\venv\Scripts\python.exe F:\result\eye_first_move_to_objects_time.py Traceback (most recent call last): File "F:\result\eye_first_move_to_objects_time.py", line 73, in <module> coordinate_x = float(fix_record[row_index][5].value) ValueError: could not convert string to float: '.' Error in atexit._run_exitfuncs: Traceback (most recent call last): File "F:\python_projects\venv\lib\site-packages\openpyxl\worksheet\_writer.py", line 32, in _openpyxl_shutdown os.remove(path) PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'C:\\Users\\dell\\AppData\\Local\\Temp\\openpyxl.byyckh9l' Exception ignored in: <generator object WorksheetWriter.get_stream at 0x000001FBA5104820> Traceback (most recent call last): File "F:\python_projects\venv\lib\site-packages\openpyxl\worksheet\_writer.py", line 300, in get_stream File "src\lxml\serializer.pxi", line 1834, in lxml.etree._FileWriterElement.__exit__ File "src\lxml\serializer.pxi", line 1570, in lxml.etree._IncrementalFileWriter._write_end_element lxml.etree.LxmlSyntaxError: inconsistent exit action in context manager Exception ignored in: <generator object WriteOnlyWorksheet._write_rows at 0x000001FBA5104270> Traceback (most recent call last): File "F:\python_projects\venv\lib\site-packages\openpyxl\worksheet\_write_only.py", line 75, in _write_rows File "src\lxml\serializer.pxi", line 1834, in lxml.etree._FileWriterElement.__exit__ File "src\lxml\serializer.pxi", line 1568, in lxml.etree._IncrementalFileWriter._write_end_element lxml.etree.LxmlSyntaxError: not in an element Process finished with exit code 1
这是一个 Python 程序运行时出现的错误信息。其中,第一行是命令行窗口中输入的 Python 程序执行命令,指定了 Python 解释器和要执行的 Python 文件的路径。接下来是一个 traceback,它展示了程序运行时的错误信息。在这个例子中,程序在第 73 行尝试将一个字符串转换为浮点数,但该字符串是一个点,无法转换。此外,在 traceback 中还显示了一些在程序结束时出现的异常,这些异常通常与文件操作有关。在这个例子中,程序试图删除一个文件,但该文件正在被另一个程序使用,因此无法删除。最后两行 traceback 是与 lxml 库相关的异常,这些异常可能与程序中使用的 Excel 文件有关。
阅读全文
相关推荐
















