[英]Looping over columns in Python and writing the output for each column in a file
I have 7 columns in my dataset. 我的数据集中有7列。 A part of my script is taking the columns and processing it. 我脚本的一部分是处理各列并对其进行处理。 For example the following is working on second column 例如,以下内容正在第二列上工作
for line in f:
input_list.append(float(line.split()[1]))
I want it to process all 7 columns and writing each output as 'file$columnno.dat' 我希望它处理所有7列并将每个输出写为'file $ columnno.dat'
Question 1 : Is this a correct way to do it? 问题1 :这是正确的方法吗?
mylist = [1, 2, 3, 4 , 5, 6, 7]
for n in my list:
for line in f:
input_list.append(float(line.split()[n]))
Question 2 : Now the output is just a list of numbers. 问题2 :现在输出只是数字列表。
print(*closed, sep='\n')
But I want the output for each column as a file such as file1.dat (1 is the same syntax of the column no.), file2.dat
etc. Is that %f
command. 但是我希望将每一列的输出作为文件,例如file1.dat (1 is the same syntax of the column no.), file2.dat
等。这是%f
命令。 I didn't manage to fix it. 我没有解决它。 It seems pretty standard and sorry if I am overwriting this question with existing ones. 这似乎很标准,很抱歉,如果我用现有的问题覆盖这个问题。
Looks like you need list.extend
看起来您需要list.extend
Ex: 例如:
for line in f:
input_list.extend( map(float, line.split()) )
map
to convert every element in list to float 使用map
将列表中的每个元素转换为float Question 1 问题1
Your solution will not work, because you can't iterate over the same line twice, unless you use seek(0)
(see docs: Methods of File Objects ) to start again from the first line. 您的解决方案将无法正常工作,因为您无法在同一行上重复两次,除非您使用seek(0)
(请参阅docs: 文件对象的方法 )从第一行重新开始。 Instead, you can iterate each line and create a list of lists, with each sublist representing a row in your file. 相反,您可以迭代每一行并创建一个列表列表,每个子列表代表文件中的一行。
The csv
module makes the syntax easier, so you don't need to manually iterate, split strings or convert to float
. csv
模块使语法更容易,因此您无需手动进行迭代,拆分字符串或转换为float
。 These can be handled by csv.reader
in an efficient way: 这些可以由csv.reader
以有效的方式处理:
from io import StringIO
import csv
mystr = StringIO("""34.12 42.13 4.1 65.13 -42.314 54.1 45.32
0.35 65.13 76.13 17.1 -45.1 65.1 78.1""")
# replace mystr with open('file.txt', 'r')
with mystr as fin:
reader = csv.reader(fin, delimiter=' ', quoting=csv.QUOTE_NONNUMERIC)
L = list(reader)
print(L)
[[34.12, 42.13, 4.1, 65.13, -42.314, 54.1, 45.32],
[0.35, 65.13, 76.13, 17.1, -45.1, 65.1, 78.1]]
Question 2 问题2
You can iterate over each index of your list of lists via zip
. 您可以通过zip
遍历列表列表的每个索引。 Then, within your loop, iterate over values in your column. 然后,在循环中,遍历列中的值。 The output will be 7 files each with a column from the original input file. 输出将是7个文件,每个文件都有来自原始输入文件的一列。 Here's an example: 这是一个例子:
for idx, column in enumerate(zip(*L), 1):
with open(r'c:\temp\out_{0}.csv'.format(idx), 'w', newline='') as myfile:
writer = csv.writer(myfile)
for val in column:
writer.writerow([val])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.