使用爬虫工具爬数据的时候,需要大量处理url,先将数据集中属于url的那一列单独取出来,然后需要拼接某些字符串。
import sys
import csv
filename = '你的csv文件的地址'
# 里面有中文的话注意使用utf-8的格式
with open(filename, encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
header_row = next(reader)
column1 = [row[1] for row in reader]
# print(column1)
f = open("输出的文件名.txt", "w")
#将文件中的url那一列单独摘出来,并且进行换行
f.write("\n".join(map(str, column1)))
f.close()
with open("输出的文件名1.txt", "r", encoding='utf-8') as outf:
lines = outf.readlines()
//读取每一行的内容,并加上你需要拼接的字符串
for line in lines:
lineNew = line.replace('\n', '')
lineNew = lineNew + '需要拼接的字符串' + '\n'
print(lineNew)
f = open("输出的文件名2.txt", "a")
f.writelines(lineNew)
f.close()