sissivic 发表于 2020-7-10 08:08:48

我的异常处理方法呢??

https://blog.csdn.net/qq_18888869/article/details/82625343
然鹅我还是没有debug 出我的
import pandas as pd
#import csv
file = 'sp500_tickers.csv'
data =pd.read_csv(file,error_bad_lines=False)

f = open("sp500_tickers.csv","rb")#二进制格式读文件
i = 0
while True:
    i += 1
    print(i)
    line = f.readline()
    if not line:
      break
    else:
      try:
#             print(line)
#             print(line.decode('utf8'))
            line.decode('utf8')
            #为了暴露出错误,最好此处不print
      except:
            print(str(line))

name_list = data['shareName'].values.tolist()
#https://www.nasdaq.com/market-activity/stocks/aapl/news-headlines

baseurl = '//www.nasdaq.com/market-activity/stocks/'
list1 = []
for i in range(len(name_list)):
    url = baseurl +str(name_list) +'/news-headlines'
    #print(url)
    #变成list,再变成dataframe
    list = list1.append(url)

file = 'D:\python\data.csv'
total_url = pd.DataFrame(data=list1)
    #print(total_url)

total_url.to_csv('D:\python\data2.csv')
# file = 'D:\python\data.csv'
# total_url.to_csv(file)

static/image/hrline/line4.png
b'Skipping line 3: expected 1 fields, saw 2\nSkipping line 7: expected 1 fields, saw 2\nSkipping line 13: expected 1 fields, saw 2\nSkipping line 17: expected 1 fields, saw 2\nSkipping line 21: expected 1 fields, saw 2\nSkipping line 26: expected 1 fields, saw 2\nSkipping line 32: expected 1 fields, saw 2\nSkipping line 34: expected 1 fields, saw 2\nSkipping line 41: expected 1 fields, saw 4\nSkipping line 44: expected 1 fields, saw 2\nSkipping line 45: expected 1 fields, saw 5\nSkipping line 48: expected 1 fields, saw 2\nSkipping line 50: expected 1 fields, saw 5\nSkipping line 53: expected 1 fields, saw 2\nSkipping line 55: expected 1 fields, saw 4\nSkipping line 56: expected 1 fields, saw 2\nSkipping line 57: expected 1 fields, saw 4\nSkipping line 58: expected 1 fields, saw 5\nSkipping line 62: expected 1 fields, saw 2\nSkipping line 63: expected 1 fields, saw 2\nSkipping line 65: expected 1 fields, saw 2\nSkipping line 70: expected 1 fields, saw 2\nSkipping line 72: expected 1 fields, saw 2\nSkipping line 73: expected 1 fields, saw 2\nSkipping line 80: expected 1 fields, saw 2\nSkipping line 87: expected 1 fields, saw 2\nSkipping line 89: expected 1 fields, saw 2\nSkipping line 91: expected 1 fields, saw 4\nSkipping line 95: expected 1 fields, saw 2\nSkipping line 97: expected 1 fields, saw 2\nSkipping line 98: expected 1 fields, saw 3\nSkipping line 100: expected 1 fields, saw 3\nSkipping line 101: expected 1 fields, saw 2\nSkipping line 105: expected 1 fields, saw 2\nSkipping line 107: expected 1 fields, saw 2\nSkipping line 112: expected 1 fields, saw 2\nSkipping line 114: expected 1 fields, saw 6\nSkipping line 116: expected 1 fields, saw 2\nSkipping line 119: expected 1 fields, saw 4\nSkipping line 124: expected 1 fields, saw 4\nSkipping line 125: expected 1 fields, saw 2\nSkipping line 127: expected 1 fields, saw 4\nSkipping line 130: expected 1 fields, saw 2\nSkipping line 136: expected 1 fields, saw 4\nSkipping line 137: expected 1 fields, saw 2\nSkipping line 138: expected 1 fields, saw 2\nSkipping line 143: expected 1 fields, saw 3\nSkipping line 144: expected 1 fields, saw 3\nSkipping line 146: expected 1 fields, saw 2\nSkipping line 152: expected 1 fields, saw 2\nSkipping line 161: expected 1 fields, saw 2\nSkipping line 162: expected 1 fields, saw 2\nSkipping line 163: expected 1 fields, saw 3\nSkipping line 167: expected 1 fields, saw 2\nSkipping line 168: expected 1 fields, saw 2\nSkipping line 174: expected 1 fields, saw 2\nSkipping line 177: expected 1 fields, saw 2\nSkipping line 178: expected 1 fields, saw 2\nSkipping line 179: expected 1 fields, saw 2\nSkipping line 181: expected 1 fields, saw 2\nSkipping line 183: expected 1 fields, saw 2\nSkipping line 186: expected 1 fields, saw 2\n'
Traceback (most recent call last):
File "pandas\_libs\parsers.pyx", line 1130, in pandas._libs.parsers.TextReader._convert_tokens
File "pandas\_libs\parsers.pyx", line 1254, in pandas._libs.parsers.TextReader._convert_with_dtype
File "pandas\_libs\parsers.pyx", line 1269, in pandas._libs.parsers.TextReader._string_convert
File "pandas\_libs\parsers.pyx", line 1459, in pandas._libs.parsers._string_box_utf8
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xef in position 0: invalid continuation byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/Users/owner/Desktop/爬虫学习/study_xpath/网址名.py", line 4, in <module>
    data =pd.read_csv(file,error_bad_lines=False)
File "C:\Users\owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers.py", line 676, in parser_f
    return _read(filepath_or_buffer, kwds)
File "C:\Users\owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers.py", line 454, in _read
    data = parser.read(nrows)
File "C:\Users\owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers.py", line 1133, in read
    ret = self._engine.read(nrows)
File "C:\Users\owner\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\parsers.py", line 2037, in read
    data = self._reader.read(nrows)
File "pandas\_libs\parsers.pyx", line 860, in pandas._libs.parsers.TextReader.read
File "pandas\_libs\parsers.pyx", line 875, in pandas._libs.parsers.TextReader._read_low_memory
File "pandas\_libs\parsers.pyx", line 952, in pandas._libs.parsers.TextReader._read_rows
File "pandas\_libs\parsers.pyx", line 1084, in pandas._libs.parsers.TextReader._convert_column_data
File "pandas\_libs\parsers.pyx", line 1137, in pandas._libs.parsers.TextReader._convert_tokens
File "pandas\_libs\parsers.pyx", line 1254, in pandas._libs.parsers.TextReader._convert_with_dtype
File "pandas\_libs\parsers.pyx", line 1269, in pandas._libs.parsers.TextReader._string_convert
File "pandas\_libs\parsers.pyx", line 1459, in pandas._libs.parsers._string_box_utf8
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xef in position 0: invalid continuation byte


呜呜不懂为啥!第一次都运行好了的 第二天起来就这样了

yhhpf 发表于 2020-7-10 08:39:53

while True:
    i += 1
    print(i)
    line = f.readline()
    if not line:
      break
    else:
      try:
#             print(line)
#             print(line.decode('utf8'))
            line.decode('utf8')
            #为了暴露出错误,最好此处不print
      except:
            print(str(line))

try在if not line的 else之下,not line返回为False才会执行到try,而你...没执行到吧?

Twilight6 发表于 2020-7-10 09:24:46



建议楼主用编辑器的 <> 发下代码,你的代码少

_荟桐_ 发表于 2020-7-10 20:00:44

天时地利人和
一般第一天能运行,第二天不能运行
就是缺少了天时
多行善事,多积德
便可运行成功。
善哉{:10_286:}
页: [1]
查看完整版本: 我的异常处理方法呢??