简体   繁体   English

没有特定分隔符的csv中如何读取?

[英]How to read in csv with no specific delimiter?

I have a problem. 我有个问题。 I have a csv file which has no "," as delimiter but is built as a common excel file. 我有一个不带“,”作为分隔符的csv文件,但已构建为常见的excel文件。

# 2016-01-01: Prices/Volumes for Market                 
23-24   24,57
22-23   30,1
21-22   29,52
20-21   33,07
19-20   35,34
18-19   37,41

I am only interested in reading in the second column for eg 24,57 in the first line. 我只想在第二行中阅读第一行中的24,57。 The data has no header. 数据没有标题。 How could I proceed here? 我该怎么办?

pd.read_csv(f,usecols = [2])

Does not work because I think there is no column identified. 不起作用,因为我认为没有标识的列。 Thanks for your help! 谢谢你的帮助!

Try this: 尝试这个:

pd.read_csv(f, delim_whitespace=True, names=['desired_col_name'], usecols=[1])

alternatively you might want to use pd.read_fwf 或者,您可能想使用pd.read_fwf

May be it is not suitable to read it as CSV 可能不适合以CSV格式读取

try to use regular expression, process it line by line 尝试使用正则表达式,逐行处理

https://docs.python.org/2/library/re.html https://docs.python.org/2/library/re.html

for example 例如

import re

>>> re.search('(\d{2})-(\d{2})   (\d{2}),(\d{2})', "23-24   24,57").group(1)
'23'
>>> re.search('(\d{2})-(\d{2})   (\d{2}),(\d{2})', "23-24   24,57").group(2)
'24'
>>> re.search('(\d{2})-(\d{2})   (\d{2}),(\d{2})', "23-24   24,57").group(3)
'24'
>>> re.search('(\d{2})-(\d{2})   (\d{2}),(\d{2})', "23-24   24,57").group(4)
'57'

To read file line by line in python, read this: How to read large file, line by line in python 要在python中逐行读取文件,请阅读以下内容: 如何在python中逐行读取大文件

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

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