需求:
- 讀取本地csv文件
- 取某一列包含某字符數(shù)據(jù),可用正則
- 結(jié)果寫入EXCLE
import pandas as pd
import time
curr_date = time.strftime("%Y%m%d", time.localtime())
print(curr_date)
path = "D:/code_fileAll/test_file/input/111.csv"
res_path = "D:/code_fileAll/test_file/output/"
# 讀取文件內(nèi)容,第一行不作為表頭
df = pd.read_csv(path, sep='\t', header=None, dtype=str, names=['user_id', 'book_id', 'rating', 'product_name'])
# 第一行作為表頭
# df = pd.read_csv(path, sep='\t', header=0, dtype=str)
print(df[:10])
# 讀取product_name列僅兩個(gè)中文字符的user_id和product_name 列
df_2 = df[df['product_name'].str.contains("^[一-龥]{2}$")].loc[:, ["user_id", "product_name"]]
# 讀取某列等于某字符數(shù)據(jù)
df_2 = df[df['產(chǎn)品'] == "撲克")]
print(df_2)
# 使用f 可用{}傳遞參數(shù)
res_file_name = f"res_{curr_date}.xlsx" # "res_"+curr_date+".xlsx"
print(res_file_name)
# 寫入excel
# df_2.to_excel(res_path+res_file_name, index=True) # index 表示源文件行數(shù)