提问人:Paco 提问时间:5/11/2023 最后编辑:marc_sPaco 更新时间:9/8/2023 访问量:71
使用 Python 将文本 csv 文件拆分为另一个 csv 文件,其中包含文本表示的变量
Spliting a text csv file into another csv with the variables the text represents with Python
问:
我有一个如下所示的 csv 文件(见下文)。每一行都只是文本,我想将每行拆分为它实际代表的三个变量。该文件显示客户在特定日期发表的评论及其识别号:每一行都只是文本,显示客户在某个日期对他们拥有的银行的评论。所以,我想将这个 csv 文件转换为另一个 csv 文件,它有三个变量(、、)。第一行的内容显示了我要生成的最终版本应该具有的这三个预期变量名称/列,如下所示:customer_id
date
comments
“customer_id日期评论”,,, |
“216604 2022-08-22 总体上”,这家银行是满意的。 |
《259276 2022-11-23 浙银行分行好找》,,, |
"58770 2022-03-13 ",,, |
"318031 2022-08-08 ",,, |
“380865 2022-11-20 考虑另一家银行..”,,, |
我是 Python 的绝对初学者。一个月前刚开始。所以,这可能是一个简单的任务,但我就是找不到将后者转换为三列文件的方法,如下所示:
customer_id | 日期 | 评论 |
---|---|---|
216604 | 2022-08-22 | 总的来说,这家银行是令人满意的,,, |
259276 | 2022-11-23 | 很容易找到浙银行的分行,,, |
58770 | 2022-03-13 | ,,, |
318031 | 2022-08-08 | ,,, |
380865 | 2022-11-20 | 考虑另一家银行.. |
或者,换句话说。我必须将原始文本分为三个字段:一个、一个类型和一个带有注释语料库的文本。ID
date
任何建议都非常欢迎。
谢谢。
答:
您需要拆分文本,将所需的部分分成不同的变量,然后您可以根据需要进行处理。试一试,然后根据需要进行修改:
line = "380865 2022-11-20 Seriously considerin switching to a rival bank.."
sp = line.split(" ")
id, date, text = sp[0], sp[1], " ".join(sp[2:])
print(id)
print(date)
print(text)
评论
由于您的文本没有简单的结构(它包含用于分隔的空格和其中一个字段内),因此我共享此代码以防它对您有所帮助。我已经在代码本身中包含了解释每个步骤的注释,如果它们还不够,请不要犹豫!
首先,您需要安装和模块:pandas
regex
pip install pandas
pip install regex
import regex as re
import pandas as pd
def split_line(line):
# We split the text by date (element with common structure
# in all entries YYYYY-MM-DD) using regex.
date_pattern = r"[0-9]{4}\-[0-9]{2}\-[0-9]{2}"
# We search the fields `customer_id` and `comments` by
# splitting the text with date pattern
customer_id, comments = re.split(date_pattern, line)
# We search the date number using the regex search
date = re.search(date_pattern, line).group(0)
return {
"customer_id": customer_id.strip(),
"date": date.strip(),
"comments": comments.strip()
}
if __name__ == "__main__":
# If you have the text as a python variable of type docstring
text = """"customer_id date comments
216604 2022-08-22 Overal, this bank is satisfactory,
259276 2022-11-23 Easy to find zhe bank ' s branches
380865 2022-11-20 Seriously considerin switching to a rival bank
"""
all_lines = text.split("\n")[1:]
# If you have the text as a .txt file
# with open("path/to/txt/file", "r") as f:
# all_lines = f.readlines()[1:]
# Note that we index the text lines from [1:] to remove the header
all_parsed_lanes = []
for line in all_lines:
#We measure the length of the line, eliminating spaces with .strip()
#to verify that it is not an empty line.
if len(line.strip()) > 0:
extracted_fields = split_line(line)
all_parsed_lanes.append(extracted_fields)
# We convert the list of dictionaries into a ordered and redeable
# dataframe using pandas module.
df = pd.DataFrame(all_parsed_lanes)
print(df)
返回为输出:
customer_id date comments
0 216604 2022-08-22 Overall, this bank is satisfactory,
1 259276 2022-11-23 Easy to find zhe bank ' s branches
2 380865 2022-11-20 Seriously considering switching to a rival bank
评论
你的问题很模糊。下次请问你已经尝试过什么,你的具体目标是什么。例如,您希望将输出作为嵌套列表还是字典。这是一个适用于您的特定问题的代码。但通常这样的文件应该有一些分隔符来区分哪个值属于哪个列。
该代码首先读取文件的行,并在空格处拆分。这将创建一个列表,其中前两个值是您的 ID 和日期。然后,列表的其余部分将再次加入注释。
data = []
filename = "yourfile.txt"
with open(filename) as f:
header = f.readline()[:-1]
header = header.split(" ")
data.append(header)
for line in f.readlines():
line = line[:-1].split(" ")
v1 = line[0]
v2 = line[1]
v3 = " ".join(line[2:])
data.append([v1, v2, v3])
第二个块将带有制表符的文件保存为分隔符。这也可以更改为分号。
filename = "output.csv"
with open(filename, "w") as f:
for line in data:
for val in line:
f.write(val)
f.write("\t")
f.write("\n")
评论
pd.read_csv("test.txt", delim_whitespace=True)