简体   繁体   English

将两列文件读入Python

[英]Reading a two-column file into Python

Suppose I have a data file called "test.dat" which is of the form "1 2 \\n 3 4 \\n 5 6". 假设我有一个名为“ test.dat”的数据文件,其格式为“ 1 2 \\ n 3 4 \\ n 5 6”。

If I run the following code, I get two arrays with the numbers in them: 如果运行以下代码,则会得到两个带有数字的数组:

import csv
from numpy           import *
f2 = open('test.dat', 'r') 
lines = f2.readlines()
x = []
y = [] 
for line in lines:
    p = line.split()
    x.append(float(p[0]))
    y.append(float(p[1]))
f2.close() 

print x
print y

However, I tried writing a function of the form 但是,我尝试编写以下形式的函数

def Read_Two_Column_File(file_name):
    data  = open(file_name, 'r')
    lines = data.readlines()
    x = []
    y = []
    for line in lines:
        p = line.split()
        x.append(float(p[0]))
        y.append(float(p[1]))
    data.close()
    return x, y

x, y = Read_Two_Column_File('test.dat')

print x 
print y

but this only returns the two last numbers. 但这只会返回最后两个数字。 What is going wrong here? 这是怎么了?

From your comment it would suggest that you did have mixed tab and space indenting causing your problem. 从您的评论中可以看出,您确实混用了tabspace从而导致了问题。 I could suggest a few minor changes to your code as I could see you were thinking of using the csv module: 我可以建议对您的代码进行一些小的更改,因为我看到您正在考虑使用csv模块:

Version 1 - use with to ensure the file is closed automatically 版本1 -使用with以确保文件被自动关闭

def Read_Two_Column_File(file_name):
    with open(file_name, 'r') as data:
        x = []
        y = []
        for line in data:
            p = line.split()
            x.append(float(p[0]))
            y.append(float(p[1]))

    return x, y

x, y = Read_Two_Column_File('test.dat')

print x 
print y

Version 2 - a possible solution using the csv module 版本2-使用csv模块的可能解决方案

import csv

def Read_Two_Column_File(file_name):
    with open(file_name, 'r') as f_input:
        csv_input = csv.reader(f_input, delimiter=' ', skipinitialspace=True)
        x = []
        y = []
        for cols in csv_input:
            x.append(float(cols[0]))
            y.append(float(cols[1]))

    return x, y

x, y = Read_Two_Column_File('test.dat')

print x 
print y

Both versions will display: 这两个版本都将显示:

[1.0, 3.0, 5.0]
[2.0, 4.0, 6.0]

if your file looks like that 如果你的文件看起来像那样

$ cat test.dat $ cat test.dat

1 2 \\n 3 4 \\n 5 6 1 2 \\ n 3 4 \\ n 5 6

Then your \\n is not a true \\n, so the readlines() function return the whole line '1 2 \\n 3 4 \\n 5 6' 那么您的\\ n不是真实的\\ n,因此readlines()函数返回整行'1 2 \\ n 3 4 \\ n 5 6'

Your file must look like that: 您的文件必须如下所示:

$ cat test.dat $ cat test.dat

1 2 1 2

3 4 3 4

5 6 5 6

And then your code is correct and it does work. 然后您的代码是正确的并且可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM