python-讀取txt文件并取部分列的帶某字符的數(shù)據(jù)寫到EXCEL(二)

需求:

  • 讀取本地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ù)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容