python - 从python中的文本文件中读取两列数字

标签 python python-3.x numpy

我有一个看起来像这样的文本文件(只粘贴前几行):

x   y
4   4
2   5
8   5
8   5
4   5
6   7

我需要读取此文件并绘制 x 与 y 的关系图。这是我的代码的样子:

import numpy as np
import matplotlib.pyplot as plt

with open("C:\Vikalp\Learning\Machine Learning\Practice\carstopping.txt") as f:
    next(f)
    data = f.read()

data = data.split('\n')

x = [(row.split('\t')[0]).strip() for row in data]
print(x)

y = [row.split('\t')[1] for row in data]

我的 print(x) 语句打印了很多 ascii 信息:

['\x00', '\x004\x00', '\x00', '\x002\x00', '\x00', '\x008\x00', '\x00', '\x008\x00', '\x00', '\x004\x00', '\x00', '\x006\x00', '\x00', '\x007\x00', '\x00', '\x009\x00', '\x00', '\x008\x00', '\x00', '\x001\x003\x00', '\x00', '\x001\x001\x00', '\x00', '\x005\x00', '\x00', '\x005\x00', '\x00', '\x001\x003\x00', '\x00', '\x008\x00', '\x00', '\x001\x007\x00', '\x00', '\x001\x004\x00', '\x00', '\x001\x001\x00', '\x00', '\x002\x001\x00', '\x00', '\x001\x009\x00', '\x00', '\x001\x008\x00', '\x00', '\x002\x007\x00', '\x00', '\x001\x005\x00', '\x00', '\x001\x004\x00', '\x00', '\x001\x006\x00', '\x00', '\x001\x006\x00', '\x00', '\x001\x009\x00', '\x00', '\x001\x004\x00', '\x00', '\x003\x004\x00', '\x00', '\x002\x009\x00', '\x00', '\x002\x002\x00', '\x00', '\x004\x007\x00', '\x00', '\x002\x009\x00', '\x00', '\x003\x004\x00', '\x00', '\x003\x000\x00', '\x00', '\x004\x008\x00', '\x00', '\x005\x005\x00', '\x00', '\x003\x009\x00', '\x00', '\x004\x002\x00', '\x00', '\x003\x005\x00', '\x00', '\x005\x006\x00', '\x00', '\x003\x003\x00', '\x00', '\x005\x009\x00', '\x00', '\x004\x008\x00', '\x00', '\x005\x006\x00', '\x00', '\x003\x009\x00', '\x00', '\x004\x001\x00', '\x00', '\x007\x008\x00', '\x00', '\x005\x007\x00', '\x00', '\x006\x004\x00', '\x00', '\x008\x004\x00', '\x00', '\x006\x008\x00', '\x00', '\x005\x004\x00', '\x00', '\x006\x000\x00', '\x00', '\x001\x000\x001\x00', '\x00', '\x006\x007\x00', '\x00', '\x007\x007\x00', '\x00', '\x008\x005\x00', '\x00', '\x001\x000\x007\x00', '\x00', '\x007\x009\x00', '\x00', '\x001\x003\x008\x00', '\x00', '\x001\x001\x000\x00', '\x00', '\x001\x003\x004\x00', '\x00', '\x00']

我如何摆脱所有这些特殊字符?

编辑

根据建议,我修改了我的代码如下:

import numpy as np
import matplotlib.pyplot as plt

file_data = np.genfromtxt("C:\Vikalp\Learning\Machine Learning\Practice\carstopping.txt", usecols=(0,1), skip_header=1, dtype=str)
print(file_data)
x = file_data[:,0]
print(x)

y = file_data[:,1]
print(y)

这是我在控制台中得到的:

[['\x004' '\x004']
 ['\x002' '\x005']
 ['\x008' '\x005']
 ..., 
 ['\x001\x003\x008' '\x003\x009']
 ['\x001\x001\x000' '\x004\x000']
 ['\x001\x003\x004' '\x004\x000']]
['\x004' '\x002' '\x008' ..., '\x001\x003\x008' '\x001\x001\x000'
 '\x001\x003\x004']
['\x004' '\x005' '\x005' ..., '\x003\x009' '\x004\x000' '\x004\x000']

不确定为什么我会得到所有这些字符。为了摆脱它们,我加入了以下行:

x = str(x).replace('\\x00','')
y = str(y).replace('\\x00','')

有了这个,我在控制台中得到以下输出:

[['\x004' '\x004']
 ['\x002' '\x005']
 ['\x008' '\x005']
 ..., 
 ['\x001\x003\x008' '\x003\x009']
 ['\x001\x001\x000' '\x004\x000']
 ['\x001\x003\x004' '\x004\x000']]
['4' '2' '8' ..., '138' '110'
 '134']
['4' '5' '5' ..., '39' '40' '40']

所以,x 和 y 现在是字符串列表。不确定如何将它们转换为整数。试过以下:

x = list(map(int,x))

给出这个错误:

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "C:\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Vikalp/Learning/Machine Learning/Practice/lr_practice_1.py", line 28, in <module>
    x = list(map(int,x))

ValueError: invalid literal for int() with base 10: '['

我有这三个问题:

  1. 如何处理/x00 等特殊字符以及它们出现的原因。文本文件看起来很干净。
  2. 如何将字符串列表转换为整数列表
  3. 编写此代码的最佳方式是什么?

最佳答案

您的文件是 UTF-16-LE 文件。所以你需要添加编码参数。

import numpy as np
import matplotlib.pyplot as plt
import codecs

filecp = codecs.open('carstopping.txt', encoding ='utf-16-le')
file_data = np.loadtxt(filecp, usecols=(0,1),skiprows=1)
print(file_data)
x = file_data[:,0]
print(x)

y = file_data[:,1]
print(y)

关于python - 从python中的文本文件中读取两列数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45048082/

相关文章:

python - 如何比较两个一个热编​​码列表?

Python - 将 PDF 文件保存到磁盘中

python - 使用opencv删除任何图像的背景

Python numpy.random.normal 只有正值

python - “numpy”没有属性 'core'

python - 如何为所有 NDB 数据存储条目分配默认值?

python - 使用大型 ETA 调度 celery 任务

python - 如何连接来自 3 个小整数的字节以生成由 Python 中的这些字节表示的更大数字?

python - 将向量减去每一行数据帧

list - 将数字输入到空列表 Python