python读取txt文件中的数字_在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

x = list(map(int,x))

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

请帮助解决以下三个问题:

1.如何处理/ x00之类的特殊字符,以及为什么出现这些特殊字符.文本文件似乎干净.

2.如何将字符串列表转换为int列表

3.最好编写此代码?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值