in the following code I get an error every time the line doesn't have each of the three identifiers. How can I skip the line and move to the next if the identifiers are not present in the file? If the first line does not have mfgcode, modelno, and qtyavail then the program fails. Thank you for your time.
import csv
import re
with open('file.csv', 'r') as csv_file:
csv_reader = csv.reader(csv_file)
ff = []
for line in csv_reader:
ff.append([re.search('mfgcode="(.+?)"', line[0] ).group(1),re.search('modelno="(.+?)"', line[0] ).group(1),re.search('qtyavail="(.+?)"', line[0] ).group(1)])
df = pd.DataFrame(ff,columns =['mfgcode','modelno','qtyavail'])
df.to_csv("test.csv",index=False)
print (df)
Traceback:
line 10, in <module>
ff.append([re.search('mfgcode="(.+?)"', line[0] ).group(1),re.search('modelno="(.+?)"', line[0] ).group(1),re.search('qtyavail="(.+?)"', line[0] ).group(1)])
AttributeError: 'NoneType' object has no attribute 'group'
First three lines of csvfile:
<checkresp> <header errcode="success" errmsg="sucess" />
<part branch="1" core="0.00" cost="15.69" deliverytime="1" desc="" errcode="success" kit="" linecode="nike" linenum="1" list="23.42" mfgcode="nike" modelno="1221" qtyavail="120" qtyreq="1" uom="" />
</checkresp>