1、主要就是在最开始插入的时候判断下表头或者表格是否有数据
2、用csv reader(然后主要写是a追加模式,读是r模式)
import csv with open("111.csv","a",encoding='utf-8',newline="") as f: k = csv.writer(f, dialect="excel") with open("111.csv", "r", encoding='utf-8', newline="") as f: reader = csv.reader(f) if not [row for row in reader]: k.writerow(["标题", "链接", "喜欢"]) k.writerow(list) else: k.writerow(list)
all_datas = [[1,2],[2,3],[1,2],[4,5]] for list1 in all_datas: with open(r"ablchange1.csv", 'a+', encoding='utf-8-sig', newline="") as f: k = csv.writer(f, dialect="excel") with open("ablchange1.csv", "r", encoding='utf-8-sig', newline="") as f: reader = csv.reader(f) if not [row for row in reader]: k.writerow(["a","b"]) k.writerow(list1) else: k.writerow(list1)
with open("111.txt","a") as f: for i in range(100000): f.write(str(i)+"\n") if i==9000: break
import csv with open('names.csv', 'w') as csvfile: fieldnames = ['first_name', 'last_name'] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'}) writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'}) writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
with open('specific.csv', 'a', newline="") as csvfile: fieldnames = ['ebayno', "pov", "mpn", "ipn", "opn"] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader()
file = open("aces_less.csv","w" , newline="") writer = csv.DictWriter(file, fieldnames=["asin","state"]) writer.writeheader() for i in range(len(qs)): print(qs[i]) state = getDataById(qs[i]) print("状态 = " + state + "\n") writer.writerow({"asin":qs[i], "state": state})
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理