python-读取txt文件并取部分列的带某字符的数据写到EXCEL(二)

需求:

  • 读取本地csv文件
  • 取某一列包含某字符数据,可用正则
  • 结果写入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/"
# 读取文件内容,第一行不作为表头
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列仅两个中文字符的user_id和product_name 列
df_2 = df[df['product_name'].str.contains("^[一-龥]{2}$")].loc[:, ["user_id", "product_name"]]
# 读取某列等于某字符数据
df_2 = df[df['产品'] == "扑克")]
print(df_2)
# 使用f 可用{}传递参数
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 表示源文件行数

你可能感兴趣的:(python-读取txt文件并取部分列的带某字符的数据写到EXCEL(二))