Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从文本文件读取多个数据_Python_Regex_Numpy_Pandas - Fatal编程技术网

Python 从文本文件读取多个数据

Python 从文本文件读取多个数据,python,regex,numpy,pandas,Python,Regex,Numpy,Pandas,我试图从一个文本文件中读取两条数据。以下是文件的外观: PaxHeader/data-science000755 777777 777777 00000000262 12525446741 015207 xustar00armourp000000 000000 18 gid=1050026054 17 uid=488147323 20 ctime=1431779590 20 atime=1431779720 38 LIBARCHIVE.creationtime=1431719347 23 SCH

我试图从一个文本文件中读取两条数据。以下是文件的外观:

PaxHeader/data-science000755 777777 777777 00000000262 12525446741 015207 xustar00armourp000000 000000 18 gid=1050026054
17 uid=488147323
20 ctime=1431779590
20 atime=1431779720
38 LIBARCHIVE.creationtime=1431719347
23 SCHILY.dev=16777218
24 SCHILY.ino=110226037
18 SCHILY.nlink=4
data-science/000755 Äâ{Ä>ñ F00000000000 12525446741 013547 5ustar00armourp000000 000000 data-science/PaxHeader/merged-sensor-files.csv000644 777777 777777 00000000214 12525446724 021646 xustar00armourp000000 000000 18 gid=1050026054
17 uid=488147323
20 ctime=1431779590
20 atime=1431779720
23 SCHILY.dev=16777218
24 SCHILY.ino=110226038
18 SCHILY.nlink=1
data-science/merged-sensor-files.csv000644 Äâ{Ä>ñ F00016452751 12525446724 020164 0ustar00armourp000000 000000 MTU, Time, Power, Cost, Voltage
MTU1,05/11/2015 19:59:06,4.102,0.62,122.4
MTU1,05/11/2015 19:59:05,4.089,0.62,122.3
MTU1,05/11/2015 19:59:04,4.089,0.62,122.3
MTU1,05/11/2015 19:59:06,4.089,0.62,122.3
MTU1,05/11/2015 19:59:04,4.097,0.62,122.4
MTU1,05/11/2015 19:59:03,4.097,0.62,122.4
MTU1,05/11/2015 19:59:02,4.111,0.62,122.5
MTU1,05/11/2015 19:59:03,4.111,0.62,122.5
MTU1,05/11/2015 19:59:02,4.104,0.62,122.5
MTU1,05/11/2015 19:59:01,4.090,0.62,122.4
MTU1,05/11/2015 19:59:00,4.093,0.62,122.4
MTU1,05/11/2015 19:58:59,4.112,0.62,122.5
data-science/PaxHeader/weather.json000644 777777 777777 00000000214 12525446741 017610 xustar00armourp000000 000000 18 gid=1050026054
17 uid=488147323
20 ctime=1431779590
20 atime=1431779720
23 SCHILY.dev=16777218
24 SCHILY.ino=110226039
18 SCHILY.nlink=1
data-science/weather.json000644 Äâ{Ä>ñ F00000000766 12525446741 016112 0ustar00armourp000000 000000 {"1431388800":"75.4","1431392400":"73.2","1431396000":"72.1","1431399600":"71.0", "1431403200":"70.7","1431406800":"69.6","1431410400":"69.0","1431414000":"68.8","1431417600":"69.2","1431421200":"67.9","1431424800":"68.6","1431428400":"68.7","1431432000":"72.1","1431435600":"76.2","1431439200":"80.1","1431442800":"80.7","1431446400":"80.9","1431450000":"83.3","1431453600":"84.5","1431457200":"85.1","1431460800":"87.0","1431464400":"84.2","1431468000":"84.4","1431471600":"83.0","1431475200":"81.1"}
基本上我想得到如下的值

MTU, Time, Power, Cost, Voltage
    MTU1,05/11/2015 19:59:06,4.102,0.62,122.4
作为一个单独的框架,然后是下面字典的另一个框架

{"1431388800":"75.4","1431392400":"73.2","1431396000":"72.1","1431399600":"71.0", "1431403200":"70.7","1431406800":"69.6","1431410400":"69.0","1431414000":"68.8","1431417600":"69.2","1431421200":"67.9","1431424800":"68.6","1431428400":"68.7","1431432000":"72.1","1431435600":"76.2","1431439200":"80.1","1431442800":"80.7","1431446400":"80.9","1431450000":"83.3","1431453600":"84.5","1431457200":"85.1","1431460800":"87.0","1431464400":"84.2","1431468000":"84.4","1431471600":"83.0","1431475200":"81.1"}
我可以手动剪切和复制粘贴这两个部分在单独的文件中并读入,但我想使用regex将其自动化。我想我知道如何对它进行正则化,但是在将整个文件作为文本读取时,我看到了以下值

所以我这样做了:

f=open("file",'r').read()
print(f)

'PaxHeader/data-science\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00000755 \x00777777 \x00777777 \x0000000000262 12
这些是文件的前几行。不知道为什么我经常看到\x00。这是因为使用了一些空格还是一些无法识别的字符

你知道如何得到想要的结果吗

谢谢


#是您想要的行号。

如果您需要的行号具有唯一的功能,您也可以通过搜索功能来确定行号。我不明白您的意思。我不能一直输入行号,因为文件很大。我只是给出了一段文字。有什么方法可以识别你需要的行吗?没有。我的意思是你可以数一数行。但这是一种非常低效的方法。文件很大。不仅仅是10行。
with open("file","r") as handler:
     lines=handler.readlines()
     lines[#]