运行脚本时,Python错误是特定于工作站的错误

发布于 2025-02-06 06:29:09 字数 3973 浏览 1 评论 0原文

运行Python脚本时,我会在一个工作站上遇到错误。该脚本在VM和我的工作站上运行良好。

  • PIP列表显示软件包与相同的
  • 工作站都使用Python 3.10.4 64bit
  • 这是唯一抛出此错误的工作站。

这可能是一个内存问题,但是工作站有2x4GB RAM。我试图把它弄清楚,但这也没有起作用。该文件仅1MB。

在故障排除时,我将文件切成500行,然后运行正常。当我在文件中的2500行中尝试了1000行时,它给出了相同的错误。有趣的是,工作站甚至现在只有一排也无法运行脚本。

包括error_bad_lines = falseiterator = truechunksize =low_memory = false都没有工作。

是什么导致此错误?为什么使用几行运行良好,但现在甚至没有一行?

这是追溯:

Traceback (most recent call last):
  File "c:\Users\script.py", line 5, in <module>
    data = pd.read_csv("C:/Path/file.csv", encoding='latin-1' )
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 680, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 581, in _read
    return parser.read(nrows)
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1250, in read
    index, columns, col_dict = self._engine.read(nrows)
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 225, in read
    chunks = self._reader.read_low_memory(nrows)
  File "pandas\_libs\parsers.pyx", line 805, in pandas._libs.parsers.TextReader.read_low_memory
  File "pandas\_libs\parsers.pyx", line 861, in pandas._libs.parsers.TextReader._read_rows
  File "pandas\_libs\parsers.pyx", line 847, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas\_libs\parsers.pyx", line 1960, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 5, saw 4

这是脚本:

# Import raw data
data = pd.read_csv("C:/Users/Script.csv", encoding='latin-1' )

# Create array to track failed cases.
data['Test Case Failed']= ''
data = data.replace(np.nan,'')
data.insert(0, 'ID', range(0, len(data)))

# Testcase 1
data_1 = data[(data['FirstName'] == data['SRFirstName'])]
ids = data_1.index.tolist()
for i in ids:
  data.at[i,'Test Case Failed']+=', 1'

# There are 15 more test cases that preform similar tasks

# Total cases
failed = data[(data['Test Case Failed'] != '')]
passed = data[(data['Test Case Failed'] == '')]
failed['Test Case Failed'] =failed['Test Case Failed'].str[1:]
failed = failed[(failed['Test Case Failed'] != '')]

# Clean up
del failed["ID"]
del passed["ID"]

# Print results 
failed['Test Case Failed'].value_counts()
print("There was a total of",data.shape[0], "rows.", "There was" ,data.shape[0] - failed.shape[0], "rows passed and" ,failed.shape[0], "rows failed at least one test case")

# Drop unwanted columns 
redata = passed.drop(columns=['ConsCodeImpID', 'ImportID', 'Suff1', 'SRSuff2', 'Inactive', 
'AddrRegion','AddrImpID', 'AddrImpID', 'AddrImpID.2', 'AddrImpID.1', 'PhoneAddrImpID',
'PhoneAddrImpID.1', 'PhoneImpID', 'PhoneAddrImpID', 'PhoneImpID', 'PhoneType.1', 'DateTo', 
'SecondID', 'Test Case Failed', 'PhoneImpID.1'])

# Clean address  
redata['AddrLines'] = redata['AddrLines'].str.replace('Apartment ','Apt ',regex=True)
redata['AddrLines'] = redata['AddrLines'].str.replace('Apt\\.','Apt ',regex=True)
redata['AddrLines'] = redata['AddrLines'].str.replace('APT','Apt ',regex=True)
redata['AddrLines'] = redata['AddrLines'].str.replace('nApt','Apt ',regex=True)
#There's about 100 more rows of address clean up

# Output edited dropped columns  
redata.to_csv("C:/Users/cleandata.csv", index = False)
# Output failed rows
failed.to_csv("C:/Users/Failed.csv", index = False)
# Output passed rows 
passed.to_csv("C:/Users/Passed.csv", index = False)

I am getting an error on one workstation when running a Python script. The script runs fine on VMs and my workstation.

  • pip list Shows packages are the same
  • Workstations are all using Python 3.10.4 64bit
  • This is the only workstation throwing this error.

It might be a memory issue, but the workstation has 2x4Gb RAM. I tried to chunk it out, but that did not work either. The file is barely 1Mb.

As troubleshooting, I cut the file to just 500 rows, and it ran fine. When I tried 1000 rows out of the 2500 rows in the file, it gave the same error. Interestingly the workstation cannot run the script with even just one row now.

Including error_bad_lines=False, iterator=True, chunksize=, low_memory=False have all not worked.

What is causing this error? Why did it run just fine using a few rows, but now not even with one row?

Here is the Traceback:

Traceback (most recent call last):
  File "c:\Users\script.py", line 5, in <module>
    data = pd.read_csv("C:/Path/file.csv", encoding='latin-1' )
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 680, in read_csv
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 581, in _read
    return parser.read(nrows)
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1250, in read
    index, columns, col_dict = self._engine.read(nrows)
  File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 225, in read
    chunks = self._reader.read_low_memory(nrows)
  File "pandas\_libs\parsers.pyx", line 805, in pandas._libs.parsers.TextReader.read_low_memory
  File "pandas\_libs\parsers.pyx", line 861, in pandas._libs.parsers.TextReader._read_rows
  File "pandas\_libs\parsers.pyx", line 847, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas\_libs\parsers.pyx", line 1960, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 5, saw 4

Here is the script:

# Import raw data
data = pd.read_csv("C:/Users/Script.csv", encoding='latin-1' )

# Create array to track failed cases.
data['Test Case Failed']= ''
data = data.replace(np.nan,'')
data.insert(0, 'ID', range(0, len(data)))

# Testcase 1
data_1 = data[(data['FirstName'] == data['SRFirstName'])]
ids = data_1.index.tolist()
for i in ids:
  data.at[i,'Test Case Failed']+=', 1'

# There are 15 more test cases that preform similar tasks

# Total cases
failed = data[(data['Test Case Failed'] != '')]
passed = data[(data['Test Case Failed'] == '')]
failed['Test Case Failed'] =failed['Test Case Failed'].str[1:]
failed = failed[(failed['Test Case Failed'] != '')]

# Clean up
del failed["ID"]
del passed["ID"]

# Print results 
failed['Test Case Failed'].value_counts()
print("There was a total of",data.shape[0], "rows.", "There was" ,data.shape[0] - failed.shape[0], "rows passed and" ,failed.shape[0], "rows failed at least one test case")

# Drop unwanted columns 
redata = passed.drop(columns=['ConsCodeImpID', 'ImportID', 'Suff1', 'SRSuff2', 'Inactive', 
'AddrRegion','AddrImpID', 'AddrImpID', 'AddrImpID.2', 'AddrImpID.1', 'PhoneAddrImpID',
'PhoneAddrImpID.1', 'PhoneImpID', 'PhoneAddrImpID', 'PhoneImpID', 'PhoneType.1', 'DateTo', 
'SecondID', 'Test Case Failed', 'PhoneImpID.1'])

# Clean address  
redata['AddrLines'] = redata['AddrLines'].str.replace('Apartment ','Apt ',regex=True)
redata['AddrLines'] = redata['AddrLines'].str.replace('Apt\\.','Apt ',regex=True)
redata['AddrLines'] = redata['AddrLines'].str.replace('APT','Apt ',regex=True)
redata['AddrLines'] = redata['AddrLines'].str.replace('nApt','Apt ',regex=True)
#There's about 100 more rows of address clean up

# Output edited dropped columns  
redata.to_csv("C:/Users/cleandata.csv", index = False)
# Output failed rows
failed.to_csv("C:/Users/Failed.csv", index = False)
# Output passed rows 
passed.to_csv("C:/Users/Passed.csv", index = False)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2025-02-13 06:29:09

尽管在运行脚本之前从未打开该文件,但工作站正在损坏该文件。我修理了文件,它起作用了。重新安装Excel之后,我不再需要修复文件,并且可以正常运行脚本。

  • 单击文件&gt;打开。
  • 单击包含损坏工作簿的位置和文件夹。
  • 在“打开对话框”中,选择损坏的工作簿。
  • 单击“打开”按钮旁边的箭头,然后单击打开并
    维修。
  • 打开和维修命令
  • 以恢复尽可能多的工作簿数据,请选择维修。
  • 如果维修无法恢复您的数据,请选择提取数据
    从工作簿中提取值和公式。

The workstation was corrupting the file, despite never opening it before running the script. I repaired the file and it worked. After reinstalling Excel, I no longer had to repair the file and could run the script as normal.

  • Click File > Open.
  • Click the location and folder that contains the corrupted workbook.
  • In the Open dialog box, select the corrupted workbook.
  • Click the arrow next to the Open button, and then click Open and
    Repair.
  • Open and repair command
  • To recover as much of the workbook data as possible, pick Repair.
  • If Repair isn’t able to recover your data, pick Extract Data to
    extract values and formulas from the workbook.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文