pandas读写文件

  • 写入csv
python 复制代码
import pandas as pd

data = [
    {"Name": "Alice", "Age": 30, "City": "New York"},
    {"Name": "Bob", "Age": 35, "City": "Los Angeles"},
    {"Name": "Charlie", "Age": 25, "City": "Chicago"}
]

df = pd.DataFrame(data)
df.to_csv("data.csv", index=False)
  • 写入excel
python 复制代码
import pandas as pd

data = [
    {"Name": "Alice", "Age": 30, "City": "New York"},
    {"Name": "Bob", "Age": 35, "City": "Los Angeles"},
    {"Name": "Charlie", "Age": 25, "City": "Chicago"}
]

df = pd.DataFrame(data)
df.to_excel("data.xlsx", index=False)
  • 写入txt
python 复制代码
import pandas as pd

data = [
    {"Name": "Alice", "Age": 30, "City": "New York"},
    {"Name": "Bob", "Age": 35, "City": "Los Angeles"},
    {"Name": "Charlie", "Age": 25, "City": "Chicago"}
]

df = pd.DataFrame(data)
df.to_csv("data.txt", sep='\t', index=False)
  • 读取csv
python 复制代码
import pandas as pd

df = pd.read_csv('example.csv')
  • 读取excel
python 复制代码
import pandas as pd

df = pd.read_excel('demo.xlsx')
  • 读取txt
python 复制代码
import pandas as pd

txt_file = "data.txt"
df = pd.read_csv(txt_file, delimiter='\t')
相关推荐
q***33375 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
q***42825 小时前
SpringBoot Maven快速上手
spring boot·后端·maven
Victor3566 小时前
Redis(153)Redis的网络使用如何监控?
后端
码一行6 小时前
Eino AI 实战:解析 PDF 文件 & 实现 MCP Server
后端·go
Victor3566 小时前
Redis(152) Redis的CPU使用如何监控?
后端
P***84396 小时前
解决Spring Boot中Druid连接池“discard long time none received connection“警告
spring boot·后端·oracle
雨中散步撒哈拉6 小时前
17、做中学 | 初三下期 Golang文件操作
开发语言·后端·golang
倚肆6 小时前
Spring Boot CORS 配置详解:CorsConfigurationSource 全面指南
java·spring boot·后端
databook6 小时前
告别盲人摸象,数据分析的抽样方法总结
后端·python·数据分析
v***44676 小时前
springboot之集成Elasticsearch
spring boot·后端·elasticsearch