Python:循环浏览文本CSV

2024-05-15 21:51:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图与lexnlp合作,阅读我所拥有的一个法律案例的csv,以分离文本中的不同信息,如列出的所有行为、日期等

我已经完全按照lexnlp网站的指示格式化了所有内容。但是,我的csv读取不正确。我的教授建议我写一个循环来遍历csv,这样每个句子都能被阅读。在搜索了关于编写迭代循环的不同信息之后,我仍然不太明白该怎么做

我找到了这个输入 for row in text.iterrows():但我不知道应该让它运行什么操作。我问过同学们,他们似乎也迷路了。下面是我的代码。任何和所有的帮助都是有用的


url = 'https://raw.githubusercontent.com/unt-iialab/INFO5731_Spring2020/master/In_class_exercise/01-05-1%20%20Adams%20v%20Tanner.txt'
text = pd.read_csv(url,error_bad_lines=False, names=['sentence'])


#Output appears & reads  fine with this portion
#Indicates that CSV is getting read properly
print('Number of Sentences:' , len(text['sentence']))

!pip install lexnlp


#Cannot get nlp module to read csv
import lexnlp.extract.en.acts

#This Version gives back empty brackets. I believe because it is reading text as a string. 
print(lexnlp.extract.en.acts.get_act_list('text'))

#This is the format used in the number of sentences. It creates an error message.
print(lexnlp.extract.en.acts.get_act_list(text['sentence']))

#This is the format that the lexnlp site reccommends. It also creates an error message. 
print(lexnlp.extract.en.acts.get_act_list(text))




#The following are just different features of the lexnlp module that I am going to run. 
import lexnlp.extract.en.amounts
print(list(lexnlp.extract.en.amounts.get_amounts(text)))

import lexnlp.extract.en.citations
print(list(lexnlp.extract.en.citations.get_citations(text)))

import lexnlp.extract.en.entities.nltk_re
print(list(lexnlp.extract.en.entities.nltk_re.get_entities.nltk_re.get_companies(text)))

import lexnlp.extract.en.conditions
print(list(lexnlp.extract.en.conditions.get_conditions(text)))

import lexnlp.extract.en.constraints
print(list(lexnlp.extract.en.constraints.get_constraints(text)))

import lexnlp.extract.en.copyright
print(list(lexnlp.extract.en.copyright.get_copyright(text)))

import lexnlp.extract.en.courts

import lexnlp.extract.en.cusip
print(lexnlp.extract.en.cusip.get_cusip(text))

import lexnlp.extract.en.dates
print(list(lexnlp.extract.en.dates.get_dates(text)))

import lexnlp.extract.en.definitions
print(list(lexnlp.extract.en.definitions.get_definitions(text)))

import lexnlp.extract.en.distances
print(list(lexnlp.extract.en.distances.get_distances(text)))

import lexnlp.extract.en.durations
print(list(lexnlp.extract.en.durations.get_durations(text)))

import lexnlp.extract.en.money
print(list(lexnlp.extract.en.money.get_money(text)))

import lexnlp.extract.en.percents
print(list(lexnlp.extract.en.percents.get_percents(text)))

import lexnlp.extract.en.pii
print(list(lexnlp.extract.en.pii.get_pii(text)))

import lexnlp.extract.en.ratios
print(list(lexnlp.extract.en.ratios.get_ratios(text)))

import lexnlp.extract.en.regulations
print(list(lexnlp.extract.en.regulations.get_regulations(text)))

import lexnlp.extract.en.trademarks
print(list(lexnlp.extract.en.trademarks.get_trademarks(text)))

import lexnlp.extract.en.urls
print(list(lexnlp.extract.en.urls.get_urls(text)))

以下是我收到的错误代码:

<ipython-input-2-301f76c3c169> in <module>()
     19 
     20 #This is the format used in the number of sentences. It creates an error message.
---> 21 print(lexnlp.extract.en.acts.get_act_list(text['sentence']))
     22 
     23 #This is the format that the lexnlp site reccommends. It also creates an error message.

2 frames
/usr/local/lib/python3.6/dist-packages/lexnlp/extract/en/acts.py in get_acts_annotations(text)
     37 
     38 def get_acts_annotations(text: str) -> Generator[ActAnnotation, None, None]:
---> 39     for match in ACT_PARTS_RE.finditer(text):
     40         captures = match.capturesdict()
     41         act_name = ''.join(captures.get('act_name') or [])

TypeError: expected string or buffer```

Tags: csvthetextinimportgetisextract
2条回答
import pandas as pd

df = pd.read_csv('csv_file.csv', index_col=None , header=True) 

pd.read_csv(“”)取决于您使用的相对或绝对路径。 它将以数据帧的形式读取数据

请尝试以下代码:

导入csv

将open('file.csv','rb')作为csvfile: csvreader=csv.reader(csvfile,分隔符=',')

for row in csvreader:
    print(row)

“从csv文件读取的每一行都作为字符串列表返回。不执行自动数据类型转换。”

Refer

相关问题 更多 >