我有两个CSV文件。当我用Notepad ++打开它们时,我看到它们之间存在差异。
这是第一个CSV文件:
Type,Id
Task,170033
Task,170256
Task,170263
这是第二个CSV文件:
user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36'
headers = {'User-Agent': user_agent,
}
url = "https://www.zhihu.com/node/AnswerCommentListV2?params=%7B%22answer_id%22%3A%"+"2215184366"+"%22%7D"
r = requests.get(url, headers=headers, allow_redirects = True)
soup = BeautifulSoup(r.text,"lxml")
soup.prettify()
for comment in soup.find_all("div", "zm-item-comment"):
p = comment.find("a", "zg-link author-link")
print(p)
有什么区别,如何使用Python将第一个更改为第二个?
答案 0 :(得分:0)
第一个文件是空格分隔的,每个项目都是空的。 第二个文件以逗号分隔。 要转换只需使用以下代码:
out = open('02.csv','w')
with open('01.csv') as f:
for line in f:
new_str = ','.join(list(map(lambda x: x.strip('"'),line.split())))
out.write(new_str + "\n")
out.close()
答案 1 :(得分:0)
您可以使用内置的Python库csv
执行此操作:
import csv
with open('out.csv', 'w') as csvfile_write:
writer = csv.writer(csvfile_write, delimiter=',', lineterminator='\n')
with open('in.csv') as csvfile_read:
reader = csv.reader(csvfile_read, delimiter=' ', quotechar='"')
for row in reader:
writer.writerow(row)