Python 读取包含括号中的行和使用逗号分隔的值的文本文件

Python 读取包含括号中的行和使用逗号分隔的值的文本文件,python,pandas,csv,dataframe,Python,Pandas,Csv,Dataframe,我想读取一个文本文件,其中括号中的数据为行,值为列。txt文件的格式如下: (a, b, c, d) (a1, b1, (c1,c12,c13), d1) (a2, b2, (c2,c22,c23), d2) (a3, b3, (c3,c32,c33), d3) (a4, b4, (c4,c42,c43), d4) 我想要以下格式的数据: a b c d a1 b1 c1 d1 a2 b2 c2 d2 a3 b3 c3 d3 a4 b4 c4 d4 当我以csv文件的形式读取文本文件时

我想读取一个文本文件,其中括号中的数据为行,值为列。txt文件的格式如下:

(a, b, c, d) (a1, b1, (c1,c12,c13), d1) (a2, b2, (c2,c22,c23), d2) (a3, b3, (c3,c32,c33), d3) (a4, b4, (c4,c42,c43), d4)
我想要以下格式的数据:

a  b  c  d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
当我以csv文件的形式读取文本文件时,它只读取一行中的所有数据。它显示1行和所有列。
请帮我解决这个问题。

我确信有一种更圆滑、更具python风格的方法,但这里有一个快速而肮脏的函数,您可以在此基础上进行构建

def str_parser(in_str): 
     out_str = "" 
     while(in_str != ""): 
         first_idx, last_idx = in_str.index("("), in_str.index(")") 
         sub_str = in_str[first_idx+1: last_idx] 
         out_str += (sub_str.replace(",", "") + "\n") 
         in_str = in_str[last_idx+1:] 
     return out_str
输入:

print(str_parser("(a, b, c, d) (a1, b1, c1, d1) (a2, b2, c2, d2) (a3, b3, c3, d3) (a4, b4, c4, d4)"))
输出:

a b c d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4


我相信有一种更圆滑、更具python风格的方法,但这里有一个快速而肮脏的函数,您可以在此基础上进行构建

def str_parser(in_str): 
     out_str = "" 
     while(in_str != ""): 
         first_idx, last_idx = in_str.index("("), in_str.index(")") 
         sub_str = in_str[first_idx+1: last_idx] 
         out_str += (sub_str.replace(",", "") + "\n") 
         in_str = in_str[last_idx+1:] 
     return out_str
输入:

print(str_parser("(a, b, c, d) (a1, b1, c1, d1) (a2, b2, c2, d2) (a3, b3, c3, d3) (a4, b4, c4, d4)"))
输出:

a b c d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4


你能试试下面的吗。其中输入文件是您的实际文件

#!/usr/bin/python3
import re

f = open("Input_file", "r")
text=f.read()
f.close()
text2=''
if ' ' in text:
    text2 = re.sub('^\\(|,|\\)\n$|\\)$','',text)
    text2 = text2.replace(') (' , '\n')
    text2 = text2.replace('\\)','\n')
    text2 = re.sub('\\)|\\(','',text2)
    print(text2)
概念证明:假设以下是输入文件:

cat Input_file
(a, b, c, d) (a1, b1, c1, d1) (a2, b2, c2, d2) (a3, b3, c3, d3) (a4, b4, c4, d4)
(a, b, c, d) (a1, b1, c1, d1) (a2, b2, c2, d2) (a3, b3, c3, d3) (a4, b4, c4, d4)
当我们运行脚本时,输出将如下所示

./script.py
a b c d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
a b c d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4

详细说明:读取输入文件完成内容并将其保存为可变文本。现在,使用close命令关闭该文件。现在,使用for循环遍历存储在变量文本中的输入_文件的所有内容。然后在所有行中用NULL替换开始、结束\n和。现在用NULL开始替换,然后用新行替换所有事件,使输出看起来像OP的请求。

请尝试以下内容。其中输入文件是您的实际文件

#!/usr/bin/python3
import re

f = open("Input_file", "r")
text=f.read()
f.close()
text2=''
if ' ' in text:
    text2 = re.sub('^\\(|,|\\)\n$|\\)$','',text)
    text2 = text2.replace(') (' , '\n')
    text2 = text2.replace('\\)','\n')
    text2 = re.sub('\\)|\\(','',text2)
    print(text2)
概念证明:假设以下是输入文件:

cat Input_file
(a, b, c, d) (a1, b1, c1, d1) (a2, b2, c2, d2) (a3, b3, c3, d3) (a4, b4, c4, d4)
(a, b, c, d) (a1, b1, c1, d1) (a2, b2, c2, d2) (a3, b3, c3, d3) (a4, b4, c4, d4)
当我们运行脚本时,输出将如下所示

./script.py
a b c d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
a b c d
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
详细说明:读取输入文件完成内容并将其保存为可变文本。现在,使用close命令关闭该文件。现在,使用for循环遍历存储在变量文本中的输入_文件的所有内容。然后在所有行中用NULL替换开始、结束\n和。现在,用NULL开始替换,然后用新行替换所有事件,以使输出看起来像OP的请求。

使用内置熊猫函数,在大数据帧中可能更快,您可以使用:

使用熊猫的标准“read_csv”功能。 请注意lineterminator选项。 df=pd.read_csv'data.dat',sep=,,lineterminator= 重命名第一列并删除第一个字符 df.columns.values[0]=df.columns.values[0][1:] 删除第一列的左括号: df.iloc[:,0]=df.iloc[:,0].str.replace'^\?\', 删除最后一行: df=df[:-1] printdf 借助内置熊猫功能,大数据帧可能更快,您可以使用:

使用熊猫的标准“read_csv”功能。 请注意lineterminator选项。 df=pd.read_csv'data.dat',sep=,,lineterminator= 重命名第一列并删除第一个字符 df.columns.values[0]=df.columns.values[0][1:] 删除第一列的左括号: df.iloc[:,0]=df.iloc[:,0].str.replace'^\?\', 删除最后一行: df=df[:-1] printdf
读取csv文件时,您会得到什么结果?你必须更准确地告诉我你在阅读csv文件时得到了什么结果?你必须对我说得更多precise@pyOlive这个df=df[:-1]是不必要的,应该删除。它是有用的,因为文件结束时,在dataframeNote中添加一个填充了nan值的附加行。注意:我编辑了我的答案,以构建一个更健壮的正则表达式。我猜想使用df=df[:-1]将从结果中删除最后一行fa4、b4、c4、d4。虽然回答+1很好。@pyOlive这个df=df[:-1]是不必要的,应该删除。它很有用,因为文件的结尾是在dataframeNote中添加一个填充了nan值的附加行。注意:我已经编辑了我的答案以构建一个更健壮的正则表达式。使用df=df[:-1]可以从结果中删除最后一行fa4、b4、c4、d4。不过,答案不错+1。