python - 在python中写入csv文件时如何跳过一列

标签 python csv

很抱歉,如果有人问过这个问题,但是在写入 csv 文件时是否可以跳过一列?

这是我的代码:

with open("list.csv","r") as f:
    reader2 = csv.reader(f)
    for row in reader2:
        url = 'http://peopleus.intelius.com/results.php?ReportType=33&qi=0&qk=10&qp='+row
        req = urllib.request.Request(url)
        response = urllib.request.urlopen(req)
        html = response.read()
        retrieved_name = b'class="singleName">(.*?)<\/h1'
        retrieved_number = b'<div\sclass="phone">(.*?)<\/div'
        retrieved_nothing = b"(Sorry\swe\scouldn\\'t\sfind\sany\sresults)"
        if re.search(retrieved_nothing,html):
            noth = re.search(retrieved_nothing.decode('utf-8'),html.decode('utf-8')).group(1)
            add_list(phone_data, noth)
        else:
            if re.search(retrieved_name,html):
                name_found = re.search(retrieved_name.decode('utf-8'),html.decode('utf-8')).group(1)
            else:
                name_found = "No name found on peopleus.intelius.com"
            if re.search(retrieved_number,html):
                number_found = re.search(retrieved_number.decode('utf-8'),html.decode('utf-8')).group(1)
            else:
                number_found = "No number found on peopleus.intelius.com"
            add_list(phone_data, name_found, number_found)
        with open('column_skip.csv','a+', newline='') as mess:
            writ = csv.writer(mess, dialect='excel')
            writ.writerow(phone_data[-1])
        time.sleep(10)

假设 column_skip.csv 的前三行有数据,我可以让我的程序开始在第 4 列写入它的信息吗?

最佳答案

是啊,不要用csv.writer方法,写成简单的文件写操作:

  `file_path ='your_csv_file.csv'
   with open(file_path, 'w') as fp:
       #following are the data you want to write to csv
       fp.write("%s, %s, %s" % ('Name of col1', 'col2', 'col4'))
       fp.write("\n")`

我希望这有助于...

关于python - 在python中写入csv文件时如何跳过一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41726408/

相关文章:

python + sqlalchemy : calling stored procedures in a loop

python - 如何重新排列包含关系运算符的 sympy 表达式

c# - 下次打开csv文件时如何在下一行写?

javascript - 如何从 CSV 文件制作气泡图?

Python:用空行对文件进行排序会导致错误

python - App Engine 版本,内存缓存

从特定位置导入 Python(安装了多个库)

r - 读取csv文件,在一列中包含数字和字符串

c++ - 我如何从 C++ 中操作 csv

python - 如何检查网络端口是否打开?