首页 > 解决方案 > 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()

标签: pythonbeautifulsoup

解决方案


您需要通过添加一组[]()围绕它们将参数组合成一个列表或元组:

out_file.write(",".join((CX, SDX, EDX, NCX, CJX, LX, POIX, DOIX, NAAX, DOGX, NKX)) + "\n")

PS:你也可以考虑使用csv模块;你仍然需要双括号,但它会自动为你处理,and\n以及其他各种事情


推荐阅读