convertir un fichier text en data frame
Bonjour j'ai récupéré ce bout de code
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| import re
import pandas as pd
#first u have to open the file and seperate every line like below:
df = open('file.txt', "r")
lines = df.readlines()
df.close()
# remove /n at the end of each line
for index, line in enumerate(lines):
lines[index] = line.strip()
#creating a dataframe(consider u want to convert your data to 2 columns)
df_result = pd.DataFrame(columns=('first_col', 'second_col'))
i = 0
first_col = ""
second_col = ""
for line in lines:
#you can use "if" and "replace" in case you had some conditions to manipulate the txt data
if 'X' in line:
first_col = line.replace('X', "")
else:
#you have to kind of define what are the values in columns,for example second column includes:
second_col = re.sub(r' \(.*', "", line)
#this is how you create next line data
df_result.loc[i] = [first_col, second_col]
i =i+1 |
mais j'ai cette erreur
Citation:
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
merci pour votre aide