Skip to content

Commit

Permalink
修复了导出为excel时,因为相同名称文件重复导致的bug。
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddenblue committed Aug 4, 2023
1 parent 00e27c4 commit 5a774fb
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 35 deletions.
156 changes: 156 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# personal config
document/
*.txt
pubmedsoso*.xls
test.py
pubmedsql


# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
/pudmed-20230804201740.xls
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ def main():
dbpath = "./pubmedsql"
main()
# ?term=cell%2Bblood&filter=datesearch.y_1&size=20
os.system("pause")
Binary file added pubmedsoso.ico
Binary file not shown.
92 changes: 57 additions & 35 deletions save2excel.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
# -*- coding: utf-8 -*-
import sqlite3
from time import sleep
import os

import xlwt


def save2excel(dbpath):
savepath = './pudmed-%s.xls' % savetime
if os.path.exists(savepath):
print(f"指定的保存条目文件{savepath[2:]}已存在,文件重复\n\n")
confirm = str(input(f"是否删除原有的{savepath[2:]}文件, y or n\n"))
if confirm == "y" or "yes":
os.remove(savepath)
else:
print("无法保存成excel文件, 文件名重复冲突\n")
exit(-1)
tablename = 'pubmed%s' % savetime
try:
try:
Expand Down Expand Up @@ -40,17 +49,11 @@ def save2excel(dbpath):
worksheet.write(i + 1, j, savedata[i][j])
workbook.save(savepath)
print("\n爬取数据库信息保存到excel成功\n")
except:
except IOError:
print("保存excel 文件IO异常")
except Exception:
print("爬取数据库信息保存到excel失败\n")

global dbpath
dbpath='pubmedsql'
# save2excel(dbpath)

# for i in range(len(result)):
#
# time.sleep(random.randint(1,5))

def gettable(dbpath):
try:
conn = sqlite3.connect(dbpath)
Expand All @@ -66,35 +69,54 @@ def gettable(dbpath):
except:
print("数据库查询出错,请检查数据库")

if __name__ == "__main__":
global dbpath
dbpath='pubmedsql'
# save2excel(dbpath)

# for i in range(len(result)):
#
# time.sleep(random.randint(1,5))

tablelist = gettable(dbpath)
if tablelist == None:
print("目标数据库不存在或者内容为空,请检查数据库,即将退出")
sleep(1)
exit()
print("\n")
x = 99
while x != 0:
sleep(0.5)
print("当前数据库中含有以下table(数据表格) pubmed后面的数字为生成时精确到秒的时间\n", '----' * 20, '\n')
for i in range(len(tablelist)):
print("[%d]%s " % (i + 1, tablelist[i]), end='')
tablelist = gettable(dbpath)
if tablelist == None:
print("目标数据库不存在或者内容为空,请检查数据库,即将退出")
sleep(1)
exit()
print("\n")
print('----' * 20)
x = int(input("\n请输入你想要导出生成excel表格的数据库table编号,如1,2,3,4,输入0退出程序\n\n"))
if x == 0:
print("欢迎使用,程序即将结束")
x = 99
while x != 0:
sleep(0.5)
break
index = tablelist[x - 1]
# print(index)
global savetime
savetime = index[6:]
# print(savetime)
save2excel(dbpath)
print("此次保存执行完成,下一个循环")
sleep(1.5)
print('----' * 20,"\n")
print("当前目录数据库中含有以下table(数据表格)pubmed后面的数字为生成时精确到秒的时间\n", '----' * 20, '\n')
for i in range(len(tablelist)):
print("[%d]%s " % (i + 1, tablelist[i]), end='')
print("\n")
print('----' * 20)
try:
x = int(input("\n请输入你想要导出生成excel表格的数据库table编号,如1,2,3,4,输入0退出程序,注意不要输入上面的pubmedxxxxx编号\n\n"))
except ValueError:
print('----' * 20, '\n')
print("输入错误,如1,2,3,4,输入0退出程序,注意不要输入上面的pubmedxxxxx编号\n\n")
print("重新输入,下一个循环")
sleep(3)
print('----' * 20, '\n')

continue
if x == 0:
print("欢迎使用,程序即将结束")
sleep(0.5)
break
index = tablelist[x - 1]
# print(index)
global savetime
savetime = index[6:]
# print(savetime)
save2excel(dbpath)
print("此次保存执行完成,下一个循环")
sleep(3)
print('----' * 20,"\n")

os.system("pause")



0 comments on commit 5a774fb

Please sign in to comment.