0
data = ['wrq~,p~,sdvvzrug,zzz edlgx frp dx\n',
        'wrq~,p~,sdv,vzrug,zzz jrrjoh frp dx\n']
for item in data:
    text1 = item.split(",")[0]
    text2 = item.split(",")[1]
    text3 = item.split(",")[2].strip()
    print(text1)
    print(text2)
    print(text3)

I'm using python to write and read files.The data was stored in comma-separated text file. Then I'm using readlines() converted to list of strings. Then use split(",") seperate by comma. However i need the output like this

text1=wrq~  
text2=p~,sdvvzrug 
text3=zzz edlgx frp dx
second string of the list will be the same 
text1=wrq~  
text2=p~,sdv,vzrug 
text3=zzz edlgx frp dx

Basic each string has three parts. either part maybe will contain some comma,so how to split string by comma and not remove the comma inside each part?

3
  • If I were you, I would change the delimiter in that file. I don't see another way around it. You can hardcode more information specific to that one file (e.g. the on line 1 string 2 has an internal comma), but that's very inefficient and troublesome. Commented Jan 26, 2022 at 11:16
  • You need to be able to define some rule that determines when a comma is a delimiter and when it isn't. The only possibility with your data would be if relevant parts of your string can be identified by their offset Commented Jan 26, 2022 at 11:24
  • Why don't you use newlines between the fields? Then you know each datapoint is made of 3 lines and you already have the output format you want. Or use CSV format with appropriate quoting (done automatically by the csv module) Commented Jan 26, 2022 at 11:25

4 Answers 4

3

Since you are reading and writing the files you could choose another delimiter instead of "," like ";". Now if you want to use "," as a delimiter you could encapsulate the strings with comma with brackets: "some_str_with_comma_inside" so that you know what is going on and treat the file differently. Finally why don't you use the csv module of python?

Sign up to request clarification or add additional context in comments.

4 Comments

No point using CSV - It won't know how to decipher that
@JCaesar csv is actually a good option, it handles well quoting (provided you use libraries to write/read it and not custom code)
Thank you @mozway Can you help me with some sample code using the csv module that will provide the OP's output from their sample data?
@JCaesar here you are
2

You can use csv with appropriate quoting, this is done automatically by the csv module:

import csv

l = [['wrq~', 'p~,sdvvzrug', 'zzz edlgx frp dx'],
     ['wrq~', 'p~,sdv,vzrug', 'zzz jrrjoh frp dx'],
    ]

## ENCODING

with open('file.csv', 'w') as f:
    writer = csv.writer(f)
    writer.writerows(l)

# file.csv
# wrq~,"p~,sdvvzrug",zzz edlgx frp dx
# wrq~,"p~,sdv,vzrug",zzz jrrjoh frp dx


## DECODING

with open('file.csv', 'r') as f:
    reader = csv.reader(f)
    l2 = list(reader)

# l2
# [['wrq~', 'p~,sdvvzrug', 'zzz edlgx frp dx'],
#  ['wrq~', 'p~,sdv,vzrug', 'zzz jrrjoh frp dx']]

2 Comments

But you've changed the format of the input data! The OP doesn't have it like that!
@JCaesar no, the input data is something OP must have BEFORE, otherwise this is impossible to unambiguously solve the problem ;)
1

This code will produce the output you've asked for but may not be a generic solution:

data = ['wrq~,p~,sdvvzrug,zzz edlgx frp dx\n', 'wrq~,p~,sdv,vzrug,zzz jrrjoh frp dx\n']

for d in data:
    t = d.split(',')
    print(t[0])
    print(','.join(t[1:-1]))
    print(t[-1])

Output:

wrq~
p~,sdvvzrug
zzz edlgx frp dx

wrq~
p~,sdv,vzrug
zzz jrrjoh frp dx

Comments

1

You seem to want text1 and text2 to be the first and last part of the comma separated list (if that's not the case, you will have to provide a rule to determine which parts should be grouped together).

You can assign the first, middle and last parts to the 3 variables using unpacking for the middle, then join the middle part back together by re-introducing the commas:

data = ['wrq~,p~,sdvvzrug,zzz edlgx frp dx\n',
        'wrq~,p~,sdv,vzrug,zzz jrrjoh frp dx\n']
for item in data:
    text1,*text2,text3 = item.strip().split(',')  # text2 will be a tuple
    text2 = ",".join(text2)                       # make it a string again
    print(text1)
    print(text2)
    print(text3)
wrq~
p~,sdvvzrug
zzz edlgx frp dx
wrq~
p~,sdv,vzrug
zzz jrrjoh frp dx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.