5 python csv web-scraping python-3.x
我想阅读以下格式的csv
BX80684I58400;https://www.websupplies.gr/epeksergastis-intel-core-i5-8400-9mb-2-80ghz-bx80684i58400
bx80677g3930;https://www.websupplies.gr/epeksergastis-intel-celeron-g3930-2mb-2-90ghz-bx80677g3930
Run Code Online (Sandbox Code Playgroud)
我使用以下
contents = []
with open('websupplies2.csv','r') as csvf: # Open file in read mode
urls = csvf.read()
split_urls=urls.split('\n')
for split_url in split_urls:
contents.append(split_url[1])
Run Code Online (Sandbox Code Playgroud)
但我明白了
字符串索引超出范围
我注意到我无法通过delimiter =';' 在csvf.read()内部。如果我将其更改为
csv.reader(csvf, delimiter=';')
Run Code Online (Sandbox Code Playgroud)
我得到不支持拆分。
感谢您的时间
使用csv模块。
前任:
import csv
with open(filename) as infile:
reader = csv.reader(infile, delimiter=";")
for row in reader:
print(row[1])
Run Code Online (Sandbox Code Playgroud)
输出:
https://www.websupplies.gr/epeksergastis-intel-core-i5-8400-9mb-2-80ghz-bx80684i58400
https://www.websupplies.gr/epeksergastis-intel-celeron-g3930-2mb-2-90ghz-bx80677g3930
Run Code Online (Sandbox Code Playgroud)