Spaces:
Runtime error
Runtime error
Commit
·
d4819df
1
Parent(s):
8e63bc3
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,20 @@
|
|
1 |
-
import
|
2 |
|
3 |
-
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import zipfile
|
2 |
|
3 |
+
# 定义要上传的zip文件名
|
4 |
+
filename = "test.zip"
|
5 |
|
6 |
+
# 将zip文件上传到Colab中,并提取到当前路径下
|
7 |
+
from gradio.inputs import FileUploader
|
8 |
+
uploader = FileUploader(label="上传zip文件")
|
9 |
+
input_zip = uploader()
|
10 |
+
with open(filename, "wb") as f:
|
11 |
+
f.write(input_zip.getvalue())
|
12 |
+
|
13 |
+
# 解压缩zip文件,并依次遍历其中的每个txt文件
|
14 |
+
with zipfile.ZipFile(filename, 'r') as zip_ref:
|
15 |
+
for name in zip_ref.namelist():
|
16 |
+
if name.endswith(".txt"): # 只处理txt文件
|
17 |
+
with zip_ref.open(name) as file:
|
18 |
+
print("当前处理的文件为:", name)
|
19 |
+
for line in file:
|
20 |
+
print(line.decode('utf-8').rstrip()) # 输出每一行的内容
|