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

2024-05-18 15:33:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个这样的文本文件(仅粘贴前几行):

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: '['

请帮助解决以下三个问题: 一。如何处理像/x00这样的特殊字符以及它们出现的原因。文本文件看起来很干净。 2。如何将字符串列表转换为int列表 三。写这段代码最好的方法是什么?


Tags: indataaslearningx00x006x000x004

热门问题