简体   繁体   English

将以逗号分隔的单词读入熊猫

[英]Reading comma-separated words into pandas

I have a file filled with words like so : 我有一个像这样的文件:

words.txt words.txt

"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O"

How can I read this file using Pandas? 如何使用Pandas读取此文件? My ultimate goal would be a series that contained (A, B, C, D, E. . .O) 我的最终目标将是一个包含(A,B,C,D,E..O)的系列

read_csv seems geared towards a table. read_csv似乎适合表。

I managed to accomplish this using 我设法做到这一点

words = list(pd.read_csv('words.txt').columns)

But this is so ugly. 但这太丑了。 I'm sure there's a better way. 我敢肯定有更好的方法。

Thank you! 谢谢!

This would be an answer 这是一个答案

list = ["A", "B", "C", "D", "E","F", "G", "H", "I", "J","K", "L", "M", "N", "O"]

print(list)

Output: 输出:

['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']

使用纯python,您可以执行以下操作来建立列表:

words = [word.strip() for line in open("words.txt") for word in line.split(",") if word]

You do not need pandas library for this alone, you can simply use the csv module for this. 您不需要单独的pandas库,只需使用csv模块即可。 Example - 范例-

import csv
with open('<csvfile>','r') as f:
    reader = csv.reader(f,skipinitialspace=True)
    words = next(reader)

skipinitialspace is to skip the whitespace after the delimiter , which seems to be there in your csv. skipinitialspace是跳过定界符之后的空白,它似乎在您的csv中。


Example/Demo - 示例/演示-

My a.csv - 我的a.csv

"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O"

Code and result - 代码和结果-

>>> import csv
>>> with open('a.csv','r') as f:
...     reader = csv.reader(f,skipinitialspace=True)
...     words = next(reader)
...
>>> words
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']

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

相关问题 在熊猫数据框中以逗号分隔的字符串中的每一项添加+1 - Add +1 to each item in a comma-separated string in pandas dataframe 将逗号分隔字符串的熊猫列转换为整数 - Converting pandas column of comma-separated strings into integers groupby逗号分隔值在单个DataFrame列python / pandas中 - groupby comma-separated values in single DataFrame column python/pandas 如何在 pandas 的单个列中合并(逗号分隔的)行值? - How to combine (comma-separated) row values in a single column in pandas? 将逗号分隔字符串的 pandas 列转换为虚拟变量 - Converting pandas column of comma-separated strings into dummy variables pandas dataframe 中将逗号分隔值转换为 integer 列表 - Convert comma-separated values into integer list in pandas dataframe 正则表达式匹配一个或多个逗号分隔的单词列表,除非字符串以逗号结尾 - Regex to match a list of one or more comma-separated words, unless the string ends in a comma 分割逗号分隔的字符串 - Splitting a comma-separated string 使用正则表达式在Pandas系列的单个单元格内使用逗号分隔值 - Separate comma-separated values within individual cells of Pandas Series using regex python pandas:将逗号分隔的列拆分为新列 - 每个值一个 - python pandas: split comma-separated column into new columns - one per value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM