国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

python批量查詢、漢字去重處理CSV文件

2020-02-15 21:35:41
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

CSV文件用記事本打開后一般為由逗號(hào)隔開的字符串,其處理方法用Python的代碼如下。為方便各種程度的人閱讀在代碼中有非常詳細(xì)的注釋。

1.查詢指定列,并保存到新的csv文件。

# -*- coding: utf-8 -*- ''''' Author: Good_Night Time: 2018/1/30 03:50 Edition: 1.0 ''' # 導(dǎo)入必須的csv庫(kù) import csv  # 創(chuàng)建臨時(shí)文件temp.csv找出所需要的列 temp_file = open("temp.csv", "w", newline='') # 如果不指定newline='',則每寫入一行將有一空行被寫入 temp_csv_writer = csv.writer(temp_file, dialect="excel") # 讀取input.csv文件,此時(shí)只有指定的一列數(shù)據(jù) with open('input.csv') as file:   temp_readcsv = csv.reader(file, delimiter=',')   for row in temp_readcsv: # 取出input.csv所有列數(shù)據(jù)     temp = [row[3]] # 得到指定列數(shù)據(jù) #    print(row[3])  #print()打印input.csv文件中第3列所有數(shù)據(jù)     temp_csv_writer.writerow(temp) # 第3列每行數(shù)據(jù)循環(huán)寫入temp.csv文件中 temp_file.close() 

2.查詢指定列中,每行數(shù)據(jù)出現(xiàn)在所有行數(shù)據(jù)的次數(shù),并保存到新的csv文件。

# -*- coding: utf-8 -*- ''''' Author: Good_Night Time: 2018/1/30 03:50 Edition: 1.0 ''' # 導(dǎo)入必須的csv庫(kù) import csv  # 創(chuàng)建臨時(shí)文件temp.csv找出所需要的列 temp_file = open("temp.csv", "w", newline='') # 如果不指定newline='',則每寫入一行將有一空行被寫入 temp_csv_writer = csv.writer(temp_file, dialect="excel") # 讀取input.csv文件,此時(shí)只有指定的一列數(shù)據(jù) with open('input.csv') as file:   temp_readcsv = csv.reader(file, delimiter=',')   for row in temp_readcsv: # 取出input.csv所有列數(shù)據(jù)     temp = [row[3]] # 得到指定列數(shù)據(jù) #    print(row[3])  #print()打印input.csv文件中第3列所有數(shù)據(jù)     temp_csv_writer.writerow(temp) # 第3列每行數(shù)據(jù)循環(huán)寫入temp.csv文件中 temp_file.close()  # 在臨時(shí)文件基礎(chǔ)上匹配所要找的數(shù)據(jù),計(jì)算出次數(shù)生成out.csv文件 flag = 0 # 臨時(shí)變量 out1 = [] # 新建數(shù)組來(lái)保存指定列的每行數(shù)據(jù) time = [] # 新建數(shù)組來(lái)保存指定列的每行數(shù)據(jù)出現(xiàn)的次數(shù) out_file = open("out.csv", "w", newline='') # 如果不指定newline='',則每寫入一行將有一空行被寫入 out_csv_writer = csv.writer(out_file, dialect="excel") out_csv_writer.writerow(["TIMES"]) # 讀取temp.csv文件,此時(shí)只有指定的一列數(shù)據(jù) with open('temp.csv') as file2:   out_readcsv = csv.reader(file2, delimiter=',')   for St in out_readcsv: # 循環(huán)取出列的每行數(shù)據(jù)     out1.append(St) # append()將列的每行數(shù)據(jù)變?yōu)閛ut1鏈表(list)的后續(xù)增加的元素,即將列數(shù)據(jù)變?yōu)橐痪S數(shù)組。 #  print(out1[1]) # 打印out1[n]的第n個(gè)元素,即原列的第n行元素   for i in range(len(out1)): # len()獲得out1鏈表(list)中元素的個(gè)數(shù),便于判斷循環(huán)次數(shù)。 #    print(out1[i]) # 打印out1鏈表所有元素,檢驗(yàn)循環(huán)是否出錯(cuò)     flag = out1.count(out1[i]) # count()獲得out1鏈表中第i個(gè)元素在所有元素中出現(xiàn)的次數(shù)。     time.append(flag) # 將獲得的某元素出現(xiàn)的次數(shù)按順序保存至time[]數(shù)組里 #  print(time) # 打印顯示所有元素出現(xiàn)的次數(shù),判斷是否出錯(cuò)   for j in range(len(out1)): # len()得到out1鏈表元素個(gè)數(shù),依此作為time[]查找下標(biāo)     times = [time[j]] # 取出元素對(duì)應(yīng)出現(xiàn)的次數(shù)     out_csv_writer.writerow(times) # 寫入out.csv文件里     print(times) # 打印顯示次數(shù) out_file.close()             
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 临泉县| 广丰县| 卫辉市| 甘孜| 固原市| 洞口县| 海淀区| 调兵山市| 东海县| 牟定县| 金秀| 探索| 班玛县| 临颍县| 思茅市| 古蔺县| 桐柏县| 六枝特区| 达州市| 尚志市| 灵武市| 彩票| 韶关市| 盐池县| 景东| 保德县| 三河市| 乳源| 沙坪坝区| 太白县| 扶绥县| 义马市| 翁牛特旗| 柘荣县| 彭水| 新龙县| 嘉峪关市| 广汉市| 汶上县| 建昌县| 平邑县|