【发布时间】:2022-01-26 08:14:54
【问题描述】:
我有一个 excel 文件,其中有 2 列:“名称”和“大小”。 'Name' 列有多种文件类型,即".apk, .dat, .vdex, .ttc" 等。但我只想填充以.apk 结尾的文件扩展名的文件。我不希望新的 Excel 文件中有任何其他文件类型。
我写了以下代码:
import pandas as pd
import json
def json_to_excel():
with open('installed-files.json') as jf:
data = json.load(jf)
df = pd.DataFrame(data)
new_df = df[df.columns.difference(['SHA256'])]
new_xl = new_df.to_excel('abc.xlsx')
return new_xl
def filter_apk(): `MODIFIED CODE`
old_xl = json_to_excel()
data = pd.read_excel(old_xl)
a = data[data["Name"].str.contains("\.apk")]
a.to_excel('zybg.xlsx')
以上程序执行以下操作:
-
json_to_excel(),获取一个 Json 文件,将其转换为 .xlsx 格式并保存。 -
filter_apk()假设根据“名称”列中存在的文件扩展名创建多个 excel 文件。
- 第一个函数正在做我打算做的事情。
- 第二个函数没有做任何事情。它也不会抛出任何错误。我关注了这个weblink
以下是“名称”列的几个示例
/system/product/<Path_to>/abc.apk
/system/fonts/wwwr.ttc
/system/framework/framework.jar
/system/<Path_to>/icu.dat
/system/<Path_to>/Normal.apk
/system/<Path_to>/Tv.apk
如何让它发挥作用?还是有更好的方法来实现目标?
请提出建议。
错误
raise ValueError(msg)
ValueError: Invalid file path or buffer object type: <class 'NoneType'>
注意:
我将所有文件都放在同一个位置。
修改代码:
import pandas as pd
import json
def json_to_excel():
with open('installed-files.json') as jf:
data = json.load(jf)
df = pd.DataFrame(data)
new_df = df[df.columns.difference(['SHA256'])]
new_df.to_excel('abc.xlsx')
def filter_apk():
json_to_excel()
old_xl = pd.read_excel('abc.xlsx')
data = pd.read_excel(old_xl)
a = data[data["Name"].str.contains("\.apk")]
a.to_excel('zybg.xlsx')
t = filter_apk()
print(t)
新错误:
Traceback (most recent call last):
File "C:/Users/amitesh.sahay/PycharmProjects/work_allocation/TASKS/Jenkins.py", line 89, in <module>
t = filter_apk()
File "C:/Users/amitesh.sahay/PycharmProjects/work_allocation/TASKS/Jenkins.py", line 84, in filter_apk
data = pd.read_excel(old_xl)
File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\util\_decorators.py", line 296, in wrapper
return func(*args, **kwargs)
File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel\_base.py", line 304, in read_excel
io = ExcelFile(io, engine=engine)
File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel\_base.py", line 867, in __init__
self._reader = self._engines[engine](self._io)
File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel\_xlrd.py", line 22, in __init__
super().__init__(filepath_or_buffer)
File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\excel\_base.py", line 344, in __init__
filepath_or_buffer, _, _, _ = get_filepath_or_buffer(filepath_or_buffer)
File "C:\Users\amitesh.sahay\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\common.py", line 243, in get_filepath_or_buffer
raise ValueError(msg)
ValueError: Invalid file path or buffer object type: <class 'pandas.core.frame.DataFrame'>
【问题讨论】:
-
为什么要先将JSON转换成excel文件?你不能直接从
'installed-files.json'开始吗? -
我这样做是因为有一个进程正在生成一个包含所有详细信息的 Json 文件。因此,作为报告的自动化过程,我必须首先将 Json 转换为 excel,然后第二个函数旨在处理特定列“名称”
-
问题是你要给新的excel文件起的名字其实是一个路径,所以python/pandas认为你想在
/system/product/<Path_to>/中保存一个名为abc.apk.xlsx的文件,所以它将其保存在那里,如果该路径不存在,则会出错。 -
好的,那么有什么解决方案可以达到目的吗?
标签: python-3.x pandas dataframe