所以我在CSV文件中有大约5008行,总共有5009个标题.我正在同一个脚本中创建和编写此文件.但是当我最后阅读它时,使用pandas pd.read_csv或python3的csv模块,并打印len,它输出4967.我检查了文件中是否有任何奇怪的字符,可能会让python感到困惑,但看不到任何.所有数据都以逗号分隔.
我也在崇高中打开它,它显示5009行而不是4967.
我可以尝试使用像merge或concat这样的pandas的其他方法,但是如果python不会读取csv的正确,那就没用了.
这是我试过的一种方法.
df1=pd.read_csv('out.csv',quoting=csv.QUOTE_NONE, error_bad_lines=False)
df2=pd.read_excel(xlsfile)
print (len(df1))#4967
print (len(df2))#5008
df2['Location']=df1['Location']
df2['Sublocation']=df1['Sublocation']
df2['Zone']=df1['Zone']
df2['Subnet Type']=df1['Subnet Type']
df2['Description']=df1['Description']
newfile = input("Enter a name for the combined csv file: ")
print('Saving to new csv file...')
df2.to_csv(newfile, index=False)
print('Done.')
target.close()
Run Code Online (Sandbox Code Playgroud)
我尝试的另一种方式是
dfcsv = pd.read_csv('out.csv')
wb = xlrd.open_workbook(xlsfile)
ws = wb.sheet_by_index(0)
xlsdata = []
for rx in range(ws.nrows):
xlsdata.append(ws.row_values(rx))
print (len(dfcsv))#4967
print (len(xlsdata))#5009
df1 = pd.DataFrame(data=dfcsv)
df2 = pd.DataFrame(data=xlsdata)
df3 = pd.concat([df2,df1], axis=1)
newfile = input("Enter a name for the combined csv …Run Code Online (Sandbox Code Playgroud)