python - Python多参数问题
问题描述
我目前在脚本中的写入行遇到问题:
out_file.write(",".join(CX, SDX, EDX, NCX, CJX, LX, POIX, DOIX, NAAX, DOGX, NKX) + "\n")
我得到的错误是
TypeError: str.join() takes exactly one argument (11 given)
我知道我只需要给出一个论点,但是我似乎无法弄清楚如何用这种方法压缩它们以适应我的 11 个标题。
任何见解将不胜感激!
编辑:
Full code:
import re
import csv
import os
from bs4 import BeautifulSoup
path = r'D:/E/F/Test1'
ext = '.htm'
out_file = open('names.csv', 'w')
out_file.write(",".join(['Ref', 'Start date of Validity', 'country', 'End date of validity', 'Nomenclature code', 'Classification justification', 'Language', 'Place of issue', 'Date of issue', 'Name and address', 'Description', 'keywords']) + "\n") # Write your keys
for filename in os.listdir(path):
if filename.endswith(ext):
fullpath = os.path.join(path, filename)
filename = os.path.splitext(os.path.basename(filename))[0]
soup = BeautifulSoup(open(fullpath, encoding="utf-8"), 'html.parser')
text = soup.get_text()
ref = soup.find("td", text="Reference")
pattern = re.compile(r'GBBTI\S{9}')
IC = soup.find("b", text="Issuing country")
CX = IC.findNext("td").contents
SD = soup.find("b", text="Start date of validity")
SDX = SD.findNext("td").contents
ED = soup.find("b", text="End date of validity")
EDX = ED.findNext("td").content
NC = soup.find("b", text="Nomenclature code")
NCX = NC.findNext("td").contents
CJ = soup.find("b", text="Classification justification")
CJX = CJ.findNext("td").contents
L = soup.find("b", text="Language")
LX = L.findNext("td").contents
POI = soup.find("b", text="Place of issue")
POIX = POI.findNext("td").contents
DOI = soup.find("b", text="Date of issue")
DOIX = DOI.findNext("td").contents
NAA = soup.find("b", text="Name and adress")
NAAX = NAA.findNext("td").contents
DOG = soup.find("b", text="Description of goods")
DOGX = DOG.findNext("td").contents
NK = soup.find("b", text="National keywords")
NKX = NK.findNext("td").contents
out_file.write(",".join((CX, SDX, EDX, NCX, CJX, LX, POIX, DOIX, NAAX, DOGX, NKX)) + "\n")
out_file.close()
解决方案
您需要通过添加一组[]
或()
围绕它们将参数组合成一个列表或元组:
out_file.write(",".join((CX, SDX, EDX, NCX, CJX, LX, POIX, DOIX, NAAX, DOGX, NKX)) + "\n")
PS:你也可以考虑使用csv
模块;你仍然需要双括号,但它会自动为你处理,
and\n
以及其他各种事情
推荐阅读
- python - 如何解决“RuntimeError: CUDA error: invalid device ordinal”?
- javascript - 固定手风琴面板宽度长度
- assembly - 如何在寄存器中的特定偏移处打印字符?
- java - Java 解析的 JSON 数组 (GSON) 返回零或 null
- python - dask是如何实现并行的?
- javascript - Highcharts中的堆叠条形图需要单个边框
- ios - 如何在 FirebaseCrashlytics iOS 最新升级中强制崩溃?
- oracle - SQL开发人员的自动跟踪执行计划中的冗余哈希连接?
- amazon-web-services - 是否有适用于 AWS 的 Microsoft Bot Composer 之类的软件?
- c++ - 使用单个可调用了解 boost::asio::post - 谁执行?