Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:循环浏览文本CSV_Python_Python 3.x_Pandas_Csv_Iteration - Fatal编程技术网

Python:循环浏览文本CSV

Python:循环浏览文本CSV,python,python-3.x,pandas,csv,iteration,Python,Python 3.x,Pandas,Csv,Iteration,我正试图与lexnlp合作,阅读我所拥有的一个法律案例的csv,以分离文本中的不同信息,如列出的所有行为、日期等 我已经完全按照lexnlp网站的指示格式化了所有内容。但是,我的csv读取不正确。我的教授建议我写一个循环来遍历csv,这样每个句子都能被阅读。在搜索了关于编写迭代循环的不同信息之后,我仍然不太明白该怎么做 我找到了这个输入 用于text.iterrows()中的行:但我不知道应该让它运行什么操作。我问过同学们,他们似乎也迷路了。下面是我的代码。任何和所有的帮助都是有用的 url

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

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

我找到了这个输入
用于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```
() 19 20#这是句子数量中使用的格式。它会创建一条错误消息。 --->21打印(lexnlp.extract.en.acts.get_act_列表(文本['句子])) 22 23#这是lexnlp网站推荐的格式。它还会创建一条错误消息。 2帧 /get_acts_注释(文本)中的usr/local/lib/python3.6/dist-packages/lexnlp/extract/en/acts.py 37 38 def get_acts_注释(文本:str)->生成器[ActAnnotation,None,None]: --->39对于行为部分中的匹配,查找器(文本): 40捕获=match.capturesdict() 41 act_name=''.join(captures.get('act_name')或[]) TypeError:应为字符串或缓冲区```
请尝试以下代码:

导入csv

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

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

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

“下面是我收到的错误代码”-所以。。。错误代码在哪里?请发布完整的回溯。我更新了上面的错误代码。您使用的url不会将您带到.csv文件,只是一个普通的文本文件。因此,使用处理表格数据的pandas没有任何意义。请参阅(及其答案),了解如何从url获取txt文件并遍历(循环)每一行。
for row in csvreader:
    print(row)
import pandas as pd

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