繁体   English   中英

如何在Linux中安装csvformat?

[英]How to install csvformat in linux?

我以前在Raspberry Pi上工作,但是一旦移至Ubuntu PC,下面的代码将无法使用。 我想我需要以某种方式安装该csvformat,但不确定如何安装。

Python代码:

import os
import subprocess
import pandas
import pandas as pd

subprocess.call("csvformat -t plates.txt >> plates.csv", shell=True)

f=pd.read_csv("plates.csv")

keep_col = [11,13]
new_f = f[keep_col]
new_f.to_csv("new_plates.csv", index=False)

subprocess.call("sudo rm plates.csv", shell=True)

df = pd.read_csv("new_plates.csv", names=["Plates", "Name"])
df["Plates"] = df["Plates"].str.split().apply("".join)

print(df)

错误信息:

/bin/sh: 1: csvformat: not found
Traceback (most recent call last):
  File "script.py", line 14, in <module>
    new_f = f[keep_col]
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py", line 2934, in __getitem__
    raise_missing=True)
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py", line 1354, in _convert_to_indexer
    return self._get_listlike_indexer(obj, axis, **kwargs)[1]
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py", line 1161, in _get_listlike_indexer
    raise_missing=raise_missing)
  File "/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py", line 1246, in _validate_read_indexer
    key=key, axis=self.obj._get_axis_name(axis)))
KeyError: "None of [Int64Index([11, 13], dtype='int64')] are in the [columns]

您有2个不同的,不相关的问题。

第一个问题:

/bin/sh: 1: csvformat: not found

你的脚本似乎是在寻找csvformat命令行工具,它是一部分csvkit

您可以使用pip安装csvkit

1.2。 安装csvkit

安装csvkit很容易:

 sudo pip install csvkit 

注意:

如果您对virtualenv熟悉,最好在自己的环境中安装csvkit。 如果执行此操作,则应保留上一个sudo中的sudo

如csvkit文档(以及注释)中所述,通常将sudo pip install视为错误。 请参阅运行“ sudo pip”有什么风险? 建议使用虚拟环境。

安装csvkit之后,请检查您现在是否具有csvformat:

csvformat -h

现在,当您运行脚本时,请确保用于安装csvkit的pip用于运行脚本的Pythonpythonpython3 ,..) 相同 例如,如果使用python运行脚本:

$ python -m pip list | grep csvkit
csvkit          1.0.4

如果csvkit没有出现,那么您将其安装在其他位置。

第二个问题:

KeyError: "None of [Int64Index([11, 13], dtype='int64')] are in the [columns]

之所以得到这个,是因为您错误地访问(切片) f的内容。 read_csv的返回read_csv pandas DataFrame。 您不能只使用f[11,13]进行切片。 您需要使用loc (基于标签的切片)或iloc (基于整数索引的切片)。

我不知道您实际上要使用keep_col = [11,13]做什么,但是您应该执行以下操作:

f=pd.read_csv("test.csv")
#     1  ..  11  12  13  ..
# 0   x  ..  x   x   x   ..
# 1   x  ..  x   x   x   ..
# 2   x  ..  x   x   x   ..
# ..

f_slice_1 = f.loc[:, '11':'13']
#     11  12  13 
# 0   x   x   x  
# 1   x   x   x  
# 2   x   x   x   
# ..

f_slice_2 = f.iloc[:, 11:13]
#     12  13 
# 0   x   x  
# 1   x   x  
# 2   x   x 
# ..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM