简体   繁体   English

如何在Python中将CSV列读取为字符串

[英]How to read a CSV column as a string in Python

I wrote code with pandas in order to pass in a CSV and retrieve a column, and then I have more code that is supposed to split the data using the re library, but it throws an error stating "TypeError: expected string or bytes-like object." 我用熊猫编写了代码,以便传递CSV并检索列,然后我有更多代码应该使用re库来拆分数据,但是它抛出错误,指出“ TypeError:预期的字符串或字节状”宾语。”

I believe I just need to convert the CSV into a string before running re on it, but I can't figure out how. 我相信我只需要在运行re之前将CSV转换为字符串即可,但是我不知道如何操作。

The column in the CSV has data which look like: 'HB1.A1D62no.0016, HB31.N33NO.89, HB 54 .N338' CSV中的列具有如下数据:“ HB1.A1D62no.0016,HB31.N33NO.89,HB 54 .N338”

import pandas as pd

data = pd.read_csv('HB_Lib.csv', delimiter = ',')
s = [data[['Call Number']]]

import re

pattern = r"(^[a-z]+)\s*(\d+(?:\.\d+)?)"
print(list(map("".join, [re.findall(pattern, part, flags=re.I)[0] for part in s])))

Traceback: 追溯:

Traceback (most recent call last):

  File "C:/Python/test2.py", line 8, in <module>
    print(list(map("".join, [re.findall(pattern, part, flags=re.I)[0] for part in s])))

  File "C:/Python/test2.py", line 8, in <listcomp>
    print(list(map("".join, [re.findall(pattern, part, flags=re.I)[0] for part in s])))

  File "C:\Python37\lib\re.py", line 223, in findall
    return _compile(pattern, flags).findall(string)

TypeError: expected string or bytes-like object
data['Call Number'] = data['Call Number'].astype(str)

I think the first thing you should do is to remove the external square brakets when declaring s. 我认为您应该做的第一件事是在声明s时除去外部方形制动器。

So, obtaining something like: 因此,获得如下内容:

a = data[['something']]

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

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