Python-从多个文件读取
我有125个数据文件,其中包含两列和21行数据。请参见下图: 我想将它们导入一个.csv文件(250列21行) 我对python相当陌生,但我得到的建议是,在代码方面:Python-从多个文件读取,python,file,Python,File,我有125个数据文件,其中包含两列和21行数据。请参见下图: 我想将它们导入一个.csv文件(250列21行) 我对python相当陌生,但我得到的建议是,在代码方面: import glob Results = [open(f) for f in glob.glob("*.data")] fout = open("res.csv", 'w') for row in range(21): for f in Results: fout.write( f.readline().strip
import glob
Results = [open(f) for f in glob.glob("*.data")]
fout = open("res.csv", 'w')
for row in range(21):
for f in Results:
fout.write( f.readline().strip() )
fout.write(',')
fout.write('\n')
fout.close()
但是,代码有点小问题,因为我只得到125列(即力和位移列写在一列中),请参考下图:
如果有人能帮我,我会非常感激的
import glob
results = [open(f) for f in glob.glob("*.data")]
sep = ","
# Uncomment if your Excel formats decimal numbers like 3,14 instead of 3.14
# sep = ";"
with open("res.csv", 'w') as fout:
for row in range(21):
iterator = (f.readline().strip().replace("\t", sep) for f in results)
line = sep.join(iterator)
fout.write("{0}\n".format(line))
因此,为了解释代码的错误,源文件使用tab作为字段分隔符,但代码使用逗号分隔从这些文件中读取的行。如果excel使用句点作为十进制分隔符,则会使用逗号作为默认字段分隔符。除非用引号括起来,否则空格将被忽略,您将看到结果
如果使用Excel的文本导入特性(数据带=>文本),您可以要求它将逗号和选项卡都看作有效字段分隔符,然后我确信您的原始输出也会起作用。
相反,上面的代码应该生成一个文件,当双击时该文件将正确打开。您不需要用python或其他语言编写自己的程序。您可以使用现有的unix命令(如果您在该环境中): 试试这个:
import glob, csv
from itertools import cycle, islice, count
def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis
pending = len(iterables)
nexts = cycle(iter(it).next for it in iterables)
while pending:
try:
for next in nexts:
yield next()
except StopIteration:
pending -= 1
nexts = cycle(islice(nexts, pending))
Results = [open(f).readlines() for f in glob.glob("*.data")]
fout = csv.writer(open("res.csv", 'wb'), dialect="excel")
row = []
for line, c in zip(roundrobin(Results), cycle(range(len(Results)))):
splitline = line.split()
for item,currItem in zip(splitline, count(1)):
row[c+currItem] = item
if count == len(Results):
fout.writerow(row)
row = []
del fout
它应该在输入文件的每一行上循环,并将它们缝合为一行,csv库将用列出的方言编写。我建议习惯csv模块。原因是,如果数据不是那么简单(标题中有简单的字符串,然后只有数字),那么很难再次实现所有内容。请尝试以下操作:
import csv
import glob
import os
datapath = './data'
resultpath = './result'
if not os.path.isdir(resultpath):
os.makedirs(resultpath)
# Initialize the empty rows. It does not check how many rows are
# in the file.
rows = []
# Read data from the files to the above matrix.
for fname in glob.glob(os.path.join(datapath, '*.data')):
with open(fname, 'rb') as f:
reader = csv.reader(f)
for n, row in enumerate(reader):
if len(rows) < n+1:
rows.append([]) # add another row
rows[n].extend(row) # append the elements from the file
# Write the data from memory to the result file.
fname = os.path.join(resultpath, 'result.csv')
with open(fname, 'wb') as f:
writer = csv.writer(f)
for row in rows:
writer.writerow(row)
csv.reader和csv.writer只是解析或组成文件行的包装器。文档说他们需要以二进制模式打开文件。您能显示用编辑器而不是excel打开的输出吗?在Excel中,不清楚哪些字符用作分隔符。另外,一个带有可视化空白的高级视图也会有所帮助(就像使用高级编辑器一样,比如说notepad++),这是一次性的吗?如果您在posix上,只需查看文件?您好,我刚刚在文本编辑器中添加了输出图像。您发布的代码看起来不可能生成上次更新中显示的输出。逗号是从哪里来的?至少,您在第一行
fout.write('\n')
中有一个缩进错误。请参阅[上一个问题][1]中我的解决方案。[1] :您好,我尝试了您的代码,但将所有内容都写在一行中作为对列的姿态。我明白了,我想知道您是否可以编辑上面的代码,因为我有点困惑。提前非常感谢。Lazyr,非常感谢,但同样的问题仍然存在。抱歉,我忘了提到它,它是tab分隔的,太棒了,我按照您的指示导入了原始输出,并将其整理好。非常感谢!。。。也就是说他在窗户上。好啊除非它已经安装好了,否则只需下载cygwin并停止胡闹。cygwin也可以使用。可能应该使用-d分隔符来正确连接行。另一方面,如果Python代码嵌入到另一个Python代码中,或者以后应该对其进行修改,那么Python代码可能更合适。
import csv
import glob
import os
datapath = './data'
resultpath = './result'
if not os.path.isdir(resultpath):
os.makedirs(resultpath)
# Initialize the empty rows. It does not check how many rows are
# in the file.
rows = []
# Read data from the files to the above matrix.
for fname in glob.glob(os.path.join(datapath, '*.data')):
with open(fname, 'rb') as f:
reader = csv.reader(f)
for n, row in enumerate(reader):
if len(rows) < n+1:
rows.append([]) # add another row
rows[n].extend(row) # append the elements from the file
# Write the data from memory to the result file.
fname = os.path.join(resultpath, 'result.csv')
with open(fname, 'wb') as f:
writer = csv.writer(f)
for row in rows:
writer.writerow(row)
f = open(fname, 'wb')
...
f.close()