arrays - 将多个文本文件合并为一个文本文件
问题描述
我想将多个文本文件合并为一个文本文件,并读取其中的所有内容。但是,这些代码只读取一个文本文件并保存从一个文本文件中获取的数据。代码;
path = "/home/Documents/Python/"
read_files = glob.glob(path+"*.txt")
with open("contents.txt", "wb") as outfile:
for f in read_files:
with open(f, "rb") as infile:
outfile.write(infile.read())
我也尝试了几乎相同的另一个代码。但是,它无法将字符串转换为浮点数,因为文本文件中的第一行包含 # 字符。
x = np.array([float(raw) for raw in f.readlines()])
我知道在 stackoverflow 中还有一些其他的问题,强调如何去做。即使我也尝试过它们,但我无法正确实现它。
我将不胜感激任何帮助或建议。
解决方案
如果有人想使用另一种方法,这些代码对我有用。我已经按照另一条路径为我自己的问题找到了解决方案。
all_files = glob.glob(path + "/*.txt")
all = []
for filename in all_files:
df = pd.read_csv(filename, index_col=None, header=0)
all.append(df)
frame = pd.concat(all, axis=0, ignore_index=True)
推荐阅读
- linux - 在c中识别虚拟网络接口
- android - 生成签名的 APK 时出错
- digital-signature - 如何在 javacard 小程序上签名并将签名返回给主机应用程序
- jquery - jQuery.validate() 错误:未捕获的 TypeError:无法读取未定义的属性“设置”
- amazon-web-services - 使用 Mandrill 邮件服务的队列 (SQS)
- php - 回声重复变量循环php
- python - 引入条件时无法使用 fillna - SyntaxError: invalid syntax
- node.js - 如何从 Firebase 函数调用第三方 Rest API 以用于 Google 上的操作
- visual-studio-code - 自动换行到窗口 VSC
- python - 重新安装 Python 后找不到 Pandas 模块