Python 转换数组中的 txt
Python converting txt in array
我正在从包含一些风传感器数据的 samba 文件服务器读取 .dat 文件。该文件包含一个 header 和一些信息(第 1,3 和 4 行),上面有传感器名称的一行(第 2 行),以及 body 上的传感器读数(144 行,1每 10 分钟一行),像这样:
"DataFormat","Anemometric tower","Datalogger","SomeCode","LoggerOS","LoggerFileSystem","AnotherCode","Table1"
"TIMESTAMP","RECORD","Precipit1","Barometer1","Temperature1","Humidity1","Anemometer1","Windvane1","Anemometer2","Windvane2","Battery1"
"TS","RN","","hPa","C. Deg","%RH","m/s","Deg","m/s","Deg","Volts"
"","","Smp","Avg","Avg","Avg","Avg","Avg","Avg","Avg","Avg"
"2019-06-19 00:10:00",1211,"NAN",921.014,19.57733,98.29526,10.76701,137.6863,10.68348,139.7062,11.91,
"2019-06-19 00:20:00",1212,"NAN",920.9402,19.44474,98.67733,9.991986,141.5792,9.892648,143.3559,11.35
"2019-06-19 00:30:00",1213,"NAN",920.6142,19.45635,99.00026,10.80979,148.0094,10.63116,150.0893,11.41
...more 141 lines...
我的目标是将原始文本转换为 table(例如,我知道第 4 列中的所有值都属于 Barometer1 传感器)。
我设法做了一个有效的 PHP 脚本,我读取所有文件直到文件末尾,附加到一个字符串,用 EOL 定界符(结果行数组)展开,最后用 ', '(逗号)分隔符(数组的数组?)
$data = '';
while (!feof($stream)) {
$data .= fread($stream, 8192);
}
$lines = explode(PHP_EOL, $data);
foreach ($lines as $line) {
$array[] = explode(",", $line);
}
然后我遍历 $array[$row][$col],为每种类型的传感器创建一个列表,并将每个列表插入相应的数据库 table。
但是我需要这个来使用 python 脚本,所以我尝试了
data = file_obj.read()
file_obj.close()
lines = data.split('\n')
array = []
for line in lines:
array[lines.index(line)] = line.split(',')
在终端 return 上打印 数据 字符串中的全文,在终端 return 上每个打印 行 行(例如:打印(行[1]))而数组return错误
array[lines.index(line)] = line.split(',')
IndexError: list assignment index out of range
使用 pysmb 库从 samba 共享中获取 file_obj
为此,最好使用 python pandas 库来组织数据框中的数据。
例如
import pandas as pd
pd.read_csv('yourfile.dat',
header=None, sep='\s\s+', engine='python')
另一种解决方案是将您的行转换为列表并将它们附加到数据框:
import pandas as pd
list=[["DataFormat","Anemometric tower","Datalogger","SomeCode","LoggerOS","LoggerFileSystem","AnotherCode","Table1"]]
df=pd.DataFrame(list,columns=['col1'])
为了编写类似的逻辑(php 和 python),我设法使用 append 使其工作
功能。
但是,就像人们提到的,熊猫图书馆也可以提供帮助。
data = file_obj.read()
file_obj.close()
lines = data.split('\n')
array = []
for line in lines:
array.append(line.split(','))
我正在从包含一些风传感器数据的 samba 文件服务器读取 .dat 文件。该文件包含一个 header 和一些信息(第 1,3 和 4 行),上面有传感器名称的一行(第 2 行),以及 body 上的传感器读数(144 行,1每 10 分钟一行),像这样:
"DataFormat","Anemometric tower","Datalogger","SomeCode","LoggerOS","LoggerFileSystem","AnotherCode","Table1"
"TIMESTAMP","RECORD","Precipit1","Barometer1","Temperature1","Humidity1","Anemometer1","Windvane1","Anemometer2","Windvane2","Battery1"
"TS","RN","","hPa","C. Deg","%RH","m/s","Deg","m/s","Deg","Volts"
"","","Smp","Avg","Avg","Avg","Avg","Avg","Avg","Avg","Avg"
"2019-06-19 00:10:00",1211,"NAN",921.014,19.57733,98.29526,10.76701,137.6863,10.68348,139.7062,11.91,
"2019-06-19 00:20:00",1212,"NAN",920.9402,19.44474,98.67733,9.991986,141.5792,9.892648,143.3559,11.35
"2019-06-19 00:30:00",1213,"NAN",920.6142,19.45635,99.00026,10.80979,148.0094,10.63116,150.0893,11.41
...more 141 lines...
我的目标是将原始文本转换为 table(例如,我知道第 4 列中的所有值都属于 Barometer1 传感器)。
我设法做了一个有效的 PHP 脚本,我读取所有文件直到文件末尾,附加到一个字符串,用 EOL 定界符(结果行数组)展开,最后用 ', '(逗号)分隔符(数组的数组?)
$data = '';
while (!feof($stream)) {
$data .= fread($stream, 8192);
}
$lines = explode(PHP_EOL, $data);
foreach ($lines as $line) {
$array[] = explode(",", $line);
}
然后我遍历 $array[$row][$col],为每种类型的传感器创建一个列表,并将每个列表插入相应的数据库 table。
但是我需要这个来使用 python 脚本,所以我尝试了
data = file_obj.read()
file_obj.close()
lines = data.split('\n')
array = []
for line in lines:
array[lines.index(line)] = line.split(',')
在终端 return 上打印 数据 字符串中的全文,在终端 return 上每个打印 行 行(例如:打印(行[1]))而数组return错误
array[lines.index(line)] = line.split(',')
IndexError: list assignment index out of range
使用 pysmb 库从 samba 共享中获取 file_obj
为此,最好使用 python pandas 库来组织数据框中的数据。
例如
import pandas as pd
pd.read_csv('yourfile.dat',
header=None, sep='\s\s+', engine='python')
另一种解决方案是将您的行转换为列表并将它们附加到数据框:
import pandas as pd
list=[["DataFormat","Anemometric tower","Datalogger","SomeCode","LoggerOS","LoggerFileSystem","AnotherCode","Table1"]]
df=pd.DataFrame(list,columns=['col1'])
为了编写类似的逻辑(php 和 python),我设法使用 append 使其工作 功能。
但是,就像人们提到的,熊猫图书馆也可以提供帮助。
data = file_obj.read()
file_obj.close()
lines = data.split('\n')
array = []
for line in lines:
array.append(line.split(','))