[英]How to deal with # sign smartly in the start of the csv file in python
这个问题似乎是答案,但是方式不同。 我想跳过前两行,因为它们只是描述,而在第三行中,我想忽略#号,而忽略数据,因为我想读取并比较此数据作为列名。
# some description here
# 1 is for good , 2 is bad and 3 for worse
# 0 temp_data 1 temp_flow 2 temp_record 3 temp_all
对于跳过行,我知道我可以做这样的事情
with open('kami.txt') as f:
lines_after_2 = f.readlines()[2:]
并读取具有相应行号或每行的文件
def read_data(data):
with open(data, 'rb') as f:
data = [row for row in csv.reader(f.readlines())]
return data
并在列名称上进行单元测试
def test_csv_read_data_headers(self):
self.assertEqual(
read_data(self.data)[0],
['temp_data 1 temp_flow 2 temp_record 3 temp_all']
)
但是由于我正在做一些单元测试,因此我想忽略#登录第三行,而不是其余的数据。
temp_data 1 temp_flow 2 temp_record 3 temp_all
任何帮助将不胜感激。 非常感谢
你尝试过熊猫吗?
import pandas as pd
df = pd.read_csv("kami.txt", header=None, skiprows = 2, names = [temp_data,
temp_flow, temp_record, temp_all])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.