【发布时间】:2021-12-30 09:00:42
【问题描述】:
目前我正在尝试自动化从 SAP 表创建 excel 文件的过程。我们总是以带有分隔符“|”的 txt 文件的形式从 SAP 系统中导入总共 50 个不同的表。不幸的是,在某些表中,此分隔符也出现在列本身中,因此在使用 pandas 时会收到错误消息。此外,我尝试使用 pandas read_fwf,但这也不起作用。关于如何解决这个问题的任何想法?此类表格的示例如下:
| Number | Date | Time | Request | Clt | Owner | User | Project | Short Text | RC |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 06.01.21 | 12:01:56 | OD1K984128 | 300 | FX5314 | F36164 | 8000013814 : P|303 CAS change interface PR SAP | ||
| 2 | 06.01.21 | 12:01:58 | OD1K984193 | 300 | FX5314 | F36164 | 8000013814 : P|303 CAS change interface PR SAP (2) | ||
| 3 | 07.01.21 | 12:07:49 | OD1K984028 | 300 | FX5310 | F36164 | 8000013709, INC0242318 change pop-up label printing | ||
| 4 | 07.01.21 | 12:07:50 | OD1K984179 | 300 | FX5370 | F36164 | 8000013834 - MRP areas in the US - Adapt ZBW_KNMT | ||
| 5 | 07.01.21 | 12:07:52 | OD1K984202 | 300 | F38091 | F36164 | 8000013839, INC0208178 Adapt spare part availability | ||
| 6 | 07.01.21 | 12:07:52 | OD1K984288 | 300 | F36131 | F36164 | 8000013862 : CCK Z004 for 7502 | ||
| 7 | 07.01.21 | 12:07:55 | OD1K984036 | 300 | F36131 | F36164 | 8000013797: storloc CN70 | ||
| 8 | 07.01.21 | 12:08:17 | OD1K984345 | 300 | FX5248 | F36164 | 8000013880, Johannfreight auth - INC0261847 | ||
| 9 | 07.01.21 | 12:08:18 | OD1K984347 | 300 | VASA10053690 | F36164 | 8000013883 2109-US6A | ||
| 10 | 07.01.21 | 12:08:18 | OD1K984005 | 300 | VASA10053690 | F36164 | 8000013884 Production Sched Profile 7502/75A2 | ||
| 11 | 07.01.21 | 12:08:22 | OD1K984360 | 300 | FX5248 | F36164 | 8000013886, Auth C088 - 3101 - F|10010 - INC0262223 | ||
| 12 | 07.01.21 | 12:08:49 | OD1K984374 | 300 | FX5248 | F36164 | 8000013889, CK13N - F39115 - INC0262507 | ||
| 13 | 07.01.21 | 12:08:50 | OD1K984351 | 300 | VASA10053690 | F36164 | 8000013884 Production Sched |Profile|7502/75A2 | ||
| 14 | 07.01.21 | 12:08:51 | OD1K984356 | 300 | FX5314 | F36164 | 8000013885 : P00303 Customs Declaration C4T (4) | ||
| 15 | 07.01.21 | 12:08:52 | OD1K984358 | 300 | FX5295 | F36164 | 8000013885, P00303 Customs Declaration C4T (4) | ||
| 16 | 07.01.21 | 12:08:52 | OD1K984381 | 300 | VASA10053690 | F36164 | 8000013893 ZAP |
如果列本身中有分隔符 ('|'),我正在尝试将数据转换为数据框无法正常工作我收到一个错误,它需要 x 列但看到 y 列。我知道错误的原因(因为列内有分隔符),但我不知道如何解决。
def exporting_xlsx(file_name, xlsx_path):
#creating the dataframe
df = pd.read_csv(file_name,
delimiter = '|',
index_col= False,
header=0,
squeeze = True,
skipinitialspace = True,
low_memory = False,
skip_blank_lines = True,
on_bad_lines='warn',
na_filter = True,
dtype=str)
#dropping empty columns
df = df.loc[:, ~df.columns.str.contains('^Unnamed')]
#exporting the file to xlsx
df.to_excel(xlsx_path, index = None, header=True)
在表上执行此代码会导致以下错误消息,因此不会导出每一行。此外,由于“|”,第 1 列和第 2 列的某些部分被放入 RC 列在列中。
b'Skipping line 14: expected 13 fields, saw 14\n'
有人知道如何解决这个问题吗?提前感谢您的所有帮助!
【问题讨论】: