我正在写一个文件,我要求别人在他们的计算机上测试它,然后他们得到一个包含数据的文件。当我运行它时,我得到一个文件,但它是空白的: 我使用了f.close()和f.flush,但仍然没有得到任何结果。
import urllib
import urllib.request
from bs4 import BeautifulSoup
import os
import string
import csv
f = open('gropn1.csv')
csv_f = csv.reader(f)
file = open(os.path.expanduser(r"grt44.csv"),"wb")
file.write(b"Partno,Category,Sub1,Sub2,Desc"+b"\n")
partno2=link2=desc2=""
for row in csv_f:
theurl="http://www.grote.com/?s="+row[1] + "&q1=1"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage,"html.parser")
for partno in soup.find_all('h2',{"class":"single-product-number"}):
partno2 = partno.text
for link in soup.find('ul',{"class":"breadcrumbs"}).findAll('a'):
link2 = link.text
for desc in soup.find_all('h3',{"class":"additional-info"}):
desc2 = desc.text
CombinedString=partno2+","+link2+","+desc2+ "\n"
file.write(bytes(CombinedString, encoding="ascii",errors='ignore'))
f.close()
非常感谢任何建议或指导。 感谢
答案 0 :(得分:0)
我刚刚创建了它,它可能包含您需要的信息:
import random
alphabet = 'abcdefghijklmnopqrstuvwxyz'
for i in range(10000):
name = random.choice(alphabet)
file = open(name+'RespectesLesConsignes{}.TXT'.format(i),'w')
for i in range(1000):
file.write(str(random.choice((1,0,3))))
file.close()
这个创建10,000个文件,但它基本上演示了如何写入文件:
#You open a new file
file = open(name+'Name{}.TXT'.format(i),'w')
#You write in it
file.write("What you want in the file")
#You close it
file.close()