#删除重复行
from pandas import read_csv;
df = read_csv('308个视频.csv')
newDF = df.drop_duplicates();
#利用python将csv文件与xlsx文件相互转化代码
##使用pandas将csv文件转成xlsx文件
方法一:
import pandas as pd
def csv_to_xlsx_pd():
csv = pd.read_csv('结果.csv',encoding='utf-8')
csv.to_excel('结果.xlsx', sheet_name='弹幕情感评分结果')
if __name__ == '__main__':
csv_to_xlsx_pd()
import pandas as pd
方法二:
import pandas as pd
data = pd.read_excel('结果.xlsx',index_col=0)
data.to_csv('结果.csv',encoding='utf-8')
方法三:
import xlrd
import csv
def xlsx_to_csv():
workbook = xlrd.open_workbook('1.xlsx')
table = workbook.sheet_by_index(0)
with codecs.open('1.csv', 'w', encoding='utf-8') as f:
write = csv.writer(f)
for row_num in range(table.nrows):
row_value = table.row_values(row_num)
write.writerow(row_value)
if __name__ == '__main__':
xlsx_to_csv()
##csv文件转xlsx文件
方法一:
import pandas as pd
def csv_to_xlsx_pd():
csv = pd.read_csv('结果.csv', encoding='utf-8')
csv.to_excel('结果.xlsx', sheet_name='data')
if __name__ == '__main__':
csv_to_xlsx_pd()
方法二:
import csv
import xlwt
def csv_to_xlsx():
with open('1.csv', 'r', encoding='utf-8') as f:
read = csv.reader(f)
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('data') # 创建一个sheet表格
l = 0
for line in read:
print(line)
r = 0
for i in line:
print(i)
sheet.write(l, r, i) # 一个一个将单元格数据写入
r = r + 1
l = l + 1
if __name__ == '__main__':
csv_to_xlsx()
方法三:
import pandas as pd
data = pd.read_csv('结果.csv',encoding='utf-8')
data.to_excel('结果.xlsx',encoding='utf-8')
【#python##python学习记录】
于 2022-10-27 11:39:18 首次发布